简体   繁体   English

C#Ajax仅发布400错误请求Firefox

[英]c# ajax post 400 bad request firefox only

I am trying to write an ajax application using C# backend. 我正在尝试使用C#后端编写ajax应用程序。 For ajax calls I am using jquery. 对于ajax调用,我正在使用jQuery。 It works fine with IE and Google Chrome but it gives 400 bad request when I try to open it using Firefox. 它可以在IE和Google Chrome上正常运行,但是当我尝试使用Firefox打开它时,它给出了400个错误的请求。 Here is the front end code 这是前端代码

$.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);
        }
    });

And I am using the following bindings in web.config if they are relevant. 如果相关,我在web.config中使用以下绑定。

    <behavior name="EndpBehavior">
      <webHttp />
    </behavior>


  <service behaviorConfiguration="ServiceBehavior" name="Service">
    <endpoint address="" binding="webHttpBinding" contract="Service" behaviorConfiguration="EndpBehavior"/>
  </service>

The function definition is as follows 函数定义如下

[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" });
}

} }

I don't understand what the mistake is. 我不明白这是什么错误。

Thanks in advance. 提前致谢。

Try to use fiddler. 尝试使用提琴手。 http://fiddler2.com . http://fiddler2.com Also use BodyStyle = WebMessageBodyStyle.Bare and Method = "POST" 还可以使用BodyStyle = WebMessageBodyStyle.Bare和Method =“ POST”

Can you try adding the following line in your service implementation, before you return the Json: 您是否可以在返回Json之前尝试在服务实现中添加以下行:

HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "x-requested-with");

After this, clear the Firefox session/cookies (or even better, open FF in 'Private browsing mode') - and hit the url again. 此后,清除Firefox会话/ cookie(甚至更好的是,在“专用浏览模式”下打开FF),然后再次点击该网址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM