Click here to Skip to main content
15,867,453 members
Articles / Productivity Apps and Services / Sharepoint
Tip/Trick

Troubleshooting an Issue With Calling an asmx Web Service in SharePoint 2010 through jQuery

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
17 Apr 2014CPOL1 min read 14.5K   3   1
Here is our tip on how to troubleshoot an issue with calling asmx web service in SharePoint 2010 through jQuery

There are several issues that can happen while migrating SharePoint from one version to another. Here is our tip on how to troubleshoot an issue with calling asmx web service in SharePoint 2010 through jQuery.

After migration of a SharePoint 2007 application to SharePoint 2010 our jQuery scripts that communicate with ASMX web services suddenly stopped working. We got the error “500 Internal Server Error”. The scripts looked as follows:

$.ajax({
  type: "POST",
  url:  "http://someserver/someapp/_layouts/Services/Products.asmx/GetProductByCountry",

  data:        JSON.stringify({ countryCode: "USA" }),
  dataType:    "json",
  contentType: 'application/json; charset=utf-8',
  context:     this,
 
  success: function (data) {
             alert(data.d);
           },
  error:   function (XMLHttpRequest, textStatus, errorThrown) {
             alert(textStatus);
           }
});

Note: The JSON object mentioned in the script is defined in the json2.js available at http://www.json.org.

This issue can be solved by adding an appropriate <webServices> section to the web.config of SharePoint application (in our case it’s located at C:\inetpub\wwwroot\wss\VirtualDirectories\80).

So, find the global <system.web> section in your web.config and make changes so that the result looks like this:

<system.web>
  <webServices>
    <protocols>
      <add name="HttpGet" />
      <add name="HttpPost" />
    </protocols>
  </webServices>
...
</system.web>

However, from the security standpoint such solution is not ideal as it allows external access through the HTTP-GET and HTTP-POST messaging protocols to all of your XML web services. So, it’s better to specify the access protocols for each web service separately, not affecting other ones. The <location> element added to the root <configuration> element provides us with this capability. The sample below defines the protocols for accessing the web service reachable by the specified path:

<configuration>
...
  <location path="_layouts/Services/Products.asmx">
    <system.web>
      <webServices>
        <protocols>
          <add name="HttpGet"/>
          <add name="HttpPost"/>
        </protocols>
      </webServices>
    </system.web>
  </location>
...
</configuration>

Note that the _layouts virtual directory is available as a subfolder of every SharePoint Web site. So if you want to limit web service usage by only one SharePoint Web site, specify the path as follows: someapp/_layouts/Services/Products.asmx.

PS Here is a good article about how to create a custom web service in SharePoint 2010.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
EastBancTechnologies
United States United States
EastBanc Technologies is a platform agnostic, full-lifecycle software development company delivering flexible technology solutions. We are scientist, mathematicians and engineers working at the frontier of technology: http://www.eastbanctech.com/

We create innovative technology solutions that increase efficiencies and maximize value by seamlessly integrating with your existing systems. Our modular approach allows for easy adaptation in a rapidly evolving environment, ultimately extending lifetime value and reducing costs.

Headquartered in Washington D.C., EastBanc Technologies has maintained an unbroken record of successful custom software architecture and development deployments since 1999. We are GSA approved and work across the commercial, enterprise, state, local, and federal government sectors.
This is a Organisation (No members)


Comments and Discussions

 
SuggestionISAPI Folder Pin
ZeBobo517-Apr-14 21:40
ZeBobo517-Apr-14 21:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.