2011-12-29 84 views
1

我正在调用具有公开JSON Web服务接口的ASP.net应用程序。如果我使用Fiddler访问Web服务,那么一切正常,我得到我想要的结果。我正在尝试使用JQuery来做同样的事情,而且我遇到了问题。我会尽可能提供尽可能多的信息,但不要太多以解决问题。使用JQuery调用JSON Web服务

在提琴手,我使用请求建设者和发送POST请求并提供以下信息:

http://www.XXXXXXXXXX.com/TASService.svc/Logon 

如果我使用Fiddler发送请求如下:

POST http://www.XXXXXXXXXX.com/TASService.svc/Logon HTTP/1.1 
content-type: application/json 
Host: www.XXXXXXXXXX.com 
Content-Length: 50 

{"Email":"[email protected]","Password":"#password1"} 

因此,我得到:

HTTP/1.1 200 OK 
Date: Thu, 29 Dec 2011 21:36:09 GMT 
Server: Microsoft-IIS/6.0 
MicrosoftOfficeWebServer: 5.0_Pub 
X-Powered-By: ASP.NET 
X-AspNet-Version: 4.0.30319 
Content-Length: 38 
Cache-Control: private 
Content-Type: application/json; charset=utf-8 

{"ContactID":52215,"Status":"Success"} 

这是一个很好的响应(成功登录)

我的HTML/JQuery的定义如下:

<html> 
<head> 
<title>jQuery Test JSON</title> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> 
</head> 
<body> 
<script type="text/javascript"> 
$(document).ready(function(){ 
$.ajax({ 
    type: "POST", 
    url: "http://www.XXXXXXXXXX.com/TASService.svc/Logon", 
    data: "{'Email':'[email protected]', 'Password':'#password1'}", 
    contentType: "application/json; charset=utf-8", 
    dataType: "jsonp", 
    success: function(msg) { 
     $('#msgid').html(msg.d); 
    }, 
    error: function (errormessage) { 
    $('#msgid').html("oops got an error!"); 
    } 
    }); 
}); 
</script> 
JSON Test Web Page 
<div id="msgid"> 
</div> 
</body> 
</html> 

现在,当我打电话运行在Firefox的网页,我得到以下(萤火虫):

"NetworkError: 405 Method Not Allowed - http://www.XXXXXXXXXX.com/TASService.svc/Logon?callback=jQuery171018036323126084708_1325195152450&{%27Email%27:%[email protected]%27,%20%27Password%27:%27#password1%27}&_=1325195152469" 

当我查看的内容发送(小提琴手RAW),我得到如下:

GET http://www.XXXXXXXXXX.com/TASService.svc/Logon?callback=jQuery171026545743770048436_1325195246881&{%27Email%27:%[email protected]%27,%20%27Password%27:%27 HTTP/1.1 
Host: www.XXXXXXXXXX.com 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1) Gecko/20100101 Firefox/8.0.1 
Accept: */* 
Accept-Language: en-us,en;q=0.5 
Accept-Encoding: gzip, deflate 
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 
Connection: keep-alive 
Pragma: no-cache 
Cache-Control: no-cache 

我注意到马上是,它发送一个GET而不是POST,虽然我指定的POST在ajax请求上。我相信这就是我得到405错误的原因。但是,我不确定它为什么要进行GET而不是POST。

任何对此的帮助将非常赞赏,因为时间不足,我的项目。

+0

我相信你只能使用GET跨域请求,我看你是使用JSONP和做一个POST。如果它是本地的,请尝试使用JSON而不是JSONP – thinkdevcode 2011-12-29 22:10:26

+0

您可以尝试删除数据参数周围的双引号吗?它应该是期待一个对象,而不是一个字符串(或评估字符串)。 – Ivan 2011-12-29 22:11:39

+0

@Ivan ASP.NET Web服务在通过jQuery调用WebMethod时确实需要字符串化数据。 – lsuarez 2011-12-29 22:26:04

回答