开发者

Calling a web service from javascript and .net 2.0

Thanks all for the help, rod.

Hi All,

Is it possible to call a web service (on my localhost) from jquery in a plain html page inside an asp.net 2.0 webform application using visual studio 2005?

<script type="text/javascript">
开发者_运维知识库    $(document).ready(function(){
        $('#button1').click(function(){                
            $('#targetDiv').load('http://localhost/testservice/Service1.asmx/HelloWorld',null,function(){alert('test')});
        });
    });
</script>

I'm getting a 500 error? Not sure if it's even possible to do this way?

thanks, rod


By default, ASP.Net doesn't enable web methods for GET requests (which is what a data-less .load() does). Scott Guthrie has a nice blog post on how to enable GET on Web Methods.

However, keep in mind that they're disabled for a reason (security mainly), you may want to just use a $.post() instead, like this:

$('#button1').click(function(){                
  $.post('http://localhost/testservice/Service1.asmx/HelloWorld',
    function(data) { $('#targetDiv').html(data); }
  );
});

Or make .load() trigger POST with a dummy data object, like this:

$('#button1').click(function(){                
  $('#targetDiv')
    .load('http://localhost/testservice/Service1.asmx/HelloWorld', {});
});

The {} is the key, passing an object makes .load() do a POST instead of a GET.


Are you sure this line is correct? http://localhost/testservice/Service1.asmx/HelloWorld Have you tried calling the webservice directly through the browser?


Enable web method in the web service so that it can be called with ajax.


Use Jquery + Ajax Instead:

http://api.jquery.com/jQuery.ajax/


A 500 error means your web service threw an exception. Look in the event log to find out what the problem is, or debug the web service to find out.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜