Json and WebMethod not working - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

Json and WebMethod not working

  • Json and WebMethod not working
  • 6a461a87-4cb5-6f77-96ac-ff0000ae3c70_page.png
    Hi,
      I worked with Sitefinity 6.1 and json with webMethod but this not working:
     Here is the jquery call that is calling the web method:
    $.ajax(
              type: "GET",
              dataType: "json",
              contentType: "application/json; charset=utf-8",
              url: baseUrl + "Processes/ItemInfo/GetItemInformation",
              data: queryString,
              success: function (data)
                   alert('info');
               ,
               error: function (response)
                              if (response)
                                  setPageLoadErrorMessage(response.toString());
                              
                          
                      );
                  );
              

     


    In the page I add a widget that only has a code with Webmethod. Here is the code for the WebMethod:
    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public static object GetItemInformation(decimal itemNumber, string imagePhysicalFileName)
             //Get information
            return new
                    
                        IsValidItem = false
                    ;

    I use a page of Sitefinity, I can't use webservice in js. I don't know if Sitefinity accepts use page with ajax and webmethod.

    I see that process and this enter in code behind, but not in webmethod, I don't Know why.

    Thanks,
    Nathalia.


  • Hello Nathalia,

    Thank you for contacting us.

    I suggest you use Firebug or Chrome built-in JavaScript debugger in order to see what the issue with this Ajax request is, what the returned errors are.

    If ItemInfo is the page which contains the WebMethod, try changing the url to baseUrl + "Processes/ItemInfo.aspx/GetItemInformation".

    Please let me know if you find this helpful and if I can assist you any further. Thank you in advance.

    Regards,
    Vasya Stankova
    Telerik
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  • 2a651a87-4cb5-6f77-96ac-ff0000ae3c70_errorpage.png
    Hi Vasya,
     Thanks for replay. I used Firebug and the ajax request not returned errors, the process only not execute the webmethod.
     If I use ItemInfo.aspx in the firebug returns a error (attach image) because the page not exist. I don't know if I should use a page.aspx  and not page of sitefinity. Can I use pages that I create in Sitefinity for webMethods?

    Thanks,
    Nathalia.
  • Hi Vasya,
                 I creted a  file .aspx in sitefinity project and the web methods is working now.

    Thanks,
    Nathalia
  • Hi Nathalia,

    Please check out the following few steps:

    1. In Sitefinity Project Manager right-click the corresponding project and click Edit in Visual Studio
    2. After the project is loaded in Visual Studio, right-click SitefinityWebApp project and click Add... and then choose WebForm
    3. Name the WebForm for instance MyWebForm.aspx
    4. Copy your  WebMethod in MyWebForm.aspx.cs
    5. Change the URL in the Ajax request to baseUrl + "MyWebForm.aspx/GetItemInformation" (baseUrl must be the path to SitefinityWebApp project folder)
    6. Build the project
    Please let me know if you have done the same. Thank you in advance.

    Regards,
    Vasya Stankova
    Telerik
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  • In regards to the baseUrl, what is the correct format?

    The path to my SitefinityWebApp project folder could be: "C:\inetpub\WebSite\Public"

    And then the rest of the url should be "MyWebForm.aspx/​WebMethod"

    But how is it supposed to be formatted or how should the final url look?

    Thanks in advance

  • i am also facing same problem, how to call method of service from control  (ChangePasswordWidget.ascx)using ajax

    1. I have build custom module, and added page widget in that in separate C#library project 

    2. Added one wcf service in custom modules Web/ Services folder\

    3.my ajax function looks like​

    <script type="text/javascript">
       
        function GetDataService()
         
            $.ajax(
                type: "POST",
                cache: false,
                url: "~/CustomModule.Web.Services/ChangePasswordService",
                data: "",
                dataType: "text",
                success: function (data)
                    console.log("ajax claaed success");
                    alert(data);
                ,
                error: function (XMLHttpRequest, textStatus, errorThrown)
                    alert(textStatus + "," + errorThrown.toString());
               

            );
       

    </script>

     

    Getting below error

    localhost:45629/.../ChangePasswordService 405 (Method Not Allowed)​

    I know issue is with Path but i don not know what path to set to access resources or service function from other custom module build in separate c# library  .

    Please help me out.

    Thanks in advance

     

     

     

     

     

     

  • Hi Anish,

    In the ajax call use either absolute or relative Url like:
    "/Sitefinity/Services/Content/EventService.svc/"

    Where is your .svc file located? You need to have the svc file in your SitefinityWebApp and reference the service from the external assembly, for instance (\Sitefinity\Services\Content\EventService.svc) :
    <%@ ServiceHost
        Language="C#"
        Debug="false"
        Service="Telerik.Sitefinity.Modules.Events.Web.Services.EventService"
        Factory="Telerik.Sitefinity.Web.Services.WcfHostFactory"
     %>

    Another option is to register the service class using Sitefinity system manager:
    SystemManager.RegisterWebService(typeof(MyService),
    "/Services/MyService.svc/");

    The third option is the service to be hosted in another web application and access it from there.

    Regards,
    Nikola Zagorchev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  • Thanks Nikola.

    One more issue i am facing is - service from  custom module (in separate c# library)is not accessible.

    Give below error - 

    POST localhost:60876/.../ChangePassord 401 (Unauthorized)

     How to authenticate service, since i am already logged despite of that giving Unauthorized error.

     

     

  • Hi Anish,

    Since the service is under /Sitefinity route, it would require backend user authentication. Either move the service to be public one, or authenticate a user that have the rights to call the service. If you want to protect certain service methods, you can use:
    ServiceUtility.RequestAuthentication();
    ServiceUtility.RequestBackendUserAuthentication();

    You should also have in mind the user might not have permissions to execute the actions in the service. In this case you should either suppress the security checks of the provider (SuppressSecurityChecks property) or use ElevatedModeRegion on the manager.
    PageManager manager = PageManager.GetManager();
    manager.Provider.SuppressSecurityChecks = true;
    ...
    manager.Provider.SuppressSecurityChecks = false;
     
    // or better use
     using (new ElevatedModeRegion(manager))
     
            ... 
     

    Hope the above info helps.

    Regards,
    Nikola Zagorchev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items