开发者

Calling a webservice using JQuery

开发者 https://www.devze.com 2023-01-08 08:55 出处:网络
I am using this code from http://www.joe-stevens.com/2010/01/04/using-jquery-to-make-ajax-calls-to-an-as开发者_如何转开发mx-web-service-using-asp-net/

I am using this code from http://www.joe-stevens.com/2010/01/04/using-jquery-to-make-ajax-calls-to-an-as开发者_如何转开发mx-web-service-using-asp-net/

function callWebService(address) {
 var result;
 $("#result").addClass("loading");
     $.ajax({
         type: "POST",
         url: address,
         data: "{}",contentType: "application/json; charset=utf-8",
         dataType: "json",
         success: Success,
         error: Error
     });
}

function Success(data, status) {
 $("#result").removeClass("loading");
 $("#result").html(data.d);
 alert("Success");
}

function Error(request, status, error) {
 $("#result").removeClass("loading");
 $("#result").html(request.statusText);
 alert("Error");
}

I don't understand what is wrong with this code. It keeps returning "Error"


Also make sure that the service URL that you're trying to access is in the same domain as your site. AJAX calls won't succeed if you cross domains, since browsers subject AJAX calls to the same domain policy. Can you also include the URL you're trying to access?

If you're trying to access a resource at a different domain, you may want to consider a JSONP request instead. See the jQuery AJAX documentation for a discussion of how to use JSONP.

I think if you combine knowing the URL you're trying to access along with Justin and mohlsen's suggestions, I think we can help.


Your code looks fine at first glance.

I recommend you use FireBug to attempt to isolate the problem further, as it will allow you to see the actual HTTP requests, POSTed data, etc...


A few suggestions based on some code I have doing this. But as others have said, make sure to manually look at the data going out and coming back. Your link references asp.net webservice, is that what you are calling since you didn't mention it.

  • Make sure the "address" url is of the form /location/page.asmx/methodname
  • You might need to pass the data to the success method in the call

    success: function(msg) {
        //msg is a json object, .d is the data field returned by asp.net
        if (msg.d.length > 0)
            ProcessData(msg.d);
        else
            HandleError('No data was returned.');
    },
     error: function() {
        HandleError('There was a problem calling the webservice.');
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号