簡體   English   中英

從javascript和.net 2.0調用Web服務

[英]Calling a web service from javascript and .net 2.0

謝謝大家的幫助吧。

大家好,

是否可以使用visual studio 2005在asp.net 2.0 webform應用程序內的簡單html頁面中從jquery調用Web服務(在我的localhost上)?

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

我收到了500錯誤? 不確定是否有可能這樣做?

謝謝,棒

默認情況下,ASP.Net不為GET請求啟用Web方法(這是無數據.load()所做的)。 Scott Guthrie有一篇關於如何在Web方法上啟用GET的博文

但是,請記住,由於某種原因(主要是安全性)它們被禁用,您可能只想使用$.post() ,如下所示:

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

或者使.load()觸發POST與虛擬數據對象,如下所示:

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

{}是鍵, 傳遞對象使.load()執行POST而不是GET

你確定這條線是正確的嗎? http://localhost/testservice/Service1.asmx/HelloWorld您是否嘗試過直接通過瀏覽器調用Web服務?

在Web服務中啟用Web方法,以便可以使用ajax調用它。

使用Jquery + Ajax代替:

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

500錯誤意味着您的Web服務引發了異常。 查看事件日志以查明問題所在,或調試Web服務以查找。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM