我正在尝试使用 C# 后端编写一个 ajax 应用程序。对于 ajax 调用,我使用的是 jquery。它适用于 IE 和 Google Chrome,但当我尝试使用 Firefox 打开它时,它会发出 400 错误请求。这是前端代码
$.ajax({
url: "http://localhost:25028/Service.svc/Fun",
type: "POST",
dataType: "json",
timeout: 10000,
contentType: "application/json; charset=utf-8",
data: JSON.stringify({input: 'input'}),
crossDomain: true,
success: function (input) {
var data = JSON.parse(input);
alert(data.data);
},
error: function (input, textstatus, errorThrown) {
alert(textstatus);
}
});
如果它们相关,我将在 web.config 中使用以下绑定。
<behavior name="EndpBehavior">
<webHttp />
</behavior>
<service behaviorConfiguration="ServiceBehavior" name="Service">
<endpoint address="" binding="webHttpBinding" contract="Service" behaviorConfiguration="EndpBehavior"/>
</service>
函数定义如下
[OperationContract]
[WebInvoke(Method = "*", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public string Fun( string input )
{
return new JavaScriptSerializer().Serialize(new { data = "this is data" });
}
}
我不明白错误是什么。
提前致谢。