+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Json and WebMethod not working

Json and WebMethod not working

10 posts, 0 answered
  1. Nathalia
    Nathalia avatar
    25 posts
    Registered:
    16 Nov 2011
    17 Sep 2013
    Link to this post
    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.


  2. Vasya Stankova
    Vasya Stankova  avatar
    12 posts
    Registered:
    03 Nov 2014
    18 Sep 2013
    Link to this post
    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
  3. Nathalia
    Nathalia avatar
    25 posts
    Registered:
    16 Nov 2011
    18 Sep 2013 in reply to Vasya Stankova
    Link to this post
    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.
  4. Vasya Stankova
    Vasya Stankova  avatar
    12 posts
    Registered:
    03 Nov 2014
    20 Sep 2013
    Link to this post
    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
  5. Nathalia
    Nathalia avatar
    25 posts
    Registered:
    16 Nov 2011
    20 Sep 2013 in reply to Vasya Stankova
    Link to this post
    Hi Vasya,
                 I creted a  file .aspx in sitefinity project and the web methods is working now.

    Thanks,
    Nathalia
  6. Terence
    Terence avatar
    4 posts
    Registered:
    09 Feb 2015
    29 Apr 2015
    Link to this post

    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

  7. Anish
    Anish avatar
    3 posts
    Registered:
    27 May 2015
    09 Jun 2015
    Link to this post

    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

    http://localhost:45629/SiteFinityVerion8/CustomModule/CustomModule.Web.Services/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

     

     

     

     

     

     

  8. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    24 Nov 2016
    12 Jun 2015
    Link to this post
    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
     
  9. Anish
    Anish avatar
    3 posts
    Registered:
    27 May 2015
    17 Jun 2015 in reply to Nikola Zagorchev
    Link to this post

    Thanks Nikola.

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

    Give below error - 

    POST http://localhost:60876/Sitefinity/Services/Content/ChangePasswordService.svc/ChangePassord 401 (Unauthorized)

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

     

     

  10. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    24 Nov 2016
    19 Jun 2015
    Link to this post
    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
     
10 posts, 0 answered