[英]jQuery Ajax Post to WCF returning “Bad Request”
I can't seem to quite get this. 我似乎不太明白这一点。
I have a WCF service like this; 我有这样的WCF服务;
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
bool Test();
Then the implementation; 然后执行;
public bool Test()
{
return true;
}
Then my jQuery; 然后是我的jQuery;
jQuery.support.cors = true;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "http://localhost:8732/Design_Time_Addresses/MyService/Service/mex/Test",
timeout: 2000,
success: function (data) {
alert(9);
},
error: function (xhr, status, error) {
alert(status);
alert(error);
}
});
Every time I call this I get "Bad Request". 每次我称此为“错误请求”。 If I change the URL to this say; 如果我将URL更改为此,请说;
http://localhost:8732/Design_Time_Addresses/MyService/Service/mex/Tesdt
i get the error "Unknown" so i think it's finding my service. 我收到错误“未知”,因此我认为它正在寻找我的服务。
You have ResponseFormat = WebMessageFormat.Json
in your server method. 您的服务器方法中有ResponseFormat = WebMessageFormat.Json
。
Yet in your AJAX call the dataType
(that you are expecting back from the server) is text
. 但是在您的AJAX调用中, dataType
(您希望从服务器返回)为text
。
TLDR: TLDR:
Change dataType: "text"
----> dataType: "json"
更改dataType: "text"
----> dataType: "json"
You usually shouldn't send the request to the "mex" endpoint, unless you configured your web endpoint address to that. 通常,除非将Web端点地址配置为该地址,否则通常不应将请求发送到“ mex”端点。 If your webHttpBinding
/ webHttpBehavior
endpoint is at http://localhost:8732/Design_Time_Addresses/MyService/Service , then the URI in your service should be just that. 如果您的webHttpBinding
/ webHttpBehavior
端点位于http:// localhost:8732 / Design_Time_Addresses / MyService / Service ,则服务中的URI应该就是这样。
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "http://localhost:8732/Design_Time_Addresses/MyService/Service/Test",
timeout: 2000,
success: function (data) {
alert(9);
},
error: function (xhr, status, error) {
alert(status);
alert(error);
}
});
Check that address, and if it doesn't work, please post your web.config and your global.asax.cs (if you're defining any routes) 检查该地址,如果该地址不起作用,请发布您的web.config和global.asax.cs(如果要定义任何路由)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.