64

I'm trying to use jQuery to get data from an ASP.NET web service (SharePoint Server 2007 lists.asmx), but any call to a web service will really help as a first step in that direction.

1
  • 1
    How would an ASP.NET service be any different from a service running on Tomcat? In other words: I don't think it matters at all what software the server is running. Commented Aug 4, 2010 at 16:22

7 Answers 7

77

I use this method as a wrapper so that I can send parameters. Also using the variables in the top of the method allows it to be minimized at a higher ratio and allows for some code reuse if making multiple similar calls.

function InfoByDate(sDate, eDate){
    var divToBeWorkedOn = "#AjaxPlaceHolder";
    var webMethod = "http://MyWebService/Web.asmx/GetInfoByDates";
    var parameters = "{'sDate':'" + sDate + "','eDate':'" + eDate + "'}";

    $.ajax({
        type: "POST",
        url: webMethod,
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            $(divToBeWorkedOn).html(msg.d);
        },
        error: function(e){
            $(divToBeWorkedOn).html("Unavailable");
        }
    });
}

Please note that this requires the 3.5 framework to expose JSON webmethods that can be consumed in this manner.

Sign up to request clarification or add additional context in comments.

6 Comments

is this line: "var webMethod = 'MyWebService/Web.asmx/GetInfoByDates'" missing a semi-colon on purpose?
Is there a way to actually view the error message? mine keeps saying unavailable but I need to view the actual error, thank you
if you use firebug or the console in chrome just store "e" in a variable and then inspect it. i.e. window.ZZZ = e;
@Bobby, I have follow the steps mentioned in your answer and I am able to call Web service. Its working fine in chrome, but when I tried the same one in firefox, I am getting 0 in response, do you have any idea ? Thanks
And how to get parameters on /Web.asmx/GetInfoByDates ??
|
9

Here is an example to call your webservice using jQuery.get:

$.get("http://domain.com/webservice.asmx", { name: "John", time: "2pm" },
  function(data){
    alert("Data Loaded: " + data);
  });

In the example above, we call "webservice.asmx", passing two parameters: name and time. Then, getting the service output in the call back function.

Comments

3

I don't know about that specific SharePoint web service, but you can decorate a page method or a web service with <WebMethod()> (in VB.NET) to ensure that it serializes to JSON. You can probably just wrap the method that webservice.asmx uses internally, in your own web service.

Dave Ward has a nice walkthrough on this.

Comments

2
$.ajax({
 type: 'POST',
 url: 'data.asmx/getText',
 data: {'argInput' : 'input arg(s)'},
 complete: function(xData, status) {
 $('#txt').html($(xData.responseXML).text()); // result
 }
});

Comments

1

SPServices is a jQuery library which abstracts SharePoint's Web Services and makes them easier to use

It is certified for SharePoint 2007

The list of supported operations for Lists.asmx could be found here

Example

In this example, we're grabbing all of the items in the Announcements list and displaying the Titles in a bulleted list in the tasksUL div:

<script type="text/javascript" src="filelink/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="filelink/jquery.SPServices-0.6.2.min.js"></script>
<script language="javascript" type="text/javascript">

$(document).ready(function() {
  $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Announcements",
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {
        var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>";
        $("#tasksUL").append(liHtml);
      });
    }
  });
});
</script>
<ul id="tasksUL"/>

Comments

1

I have a decent example in jQuery AJAX and ASMX on using the jQuery AJAX call with asmx web services...

There is a line of code to uncommment in order to have it return JSON.

1 Comment

Link is 404 not found. It's a good idea to always include the main content in the answer in case the site owner suddenly wins the lottery, divorces his wife and moves to Bahamas.
0

I quite often use ajaxpro along with jQuery. ajaxpro lets me call .NET functions from JavaScript and I use jQuery for the rest.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.