簡體   English   中英

C#Ajax僅發布400錯誤請求Firefox

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

我正在嘗試使用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" });
}

}

我不明白這是什么錯誤。

提前致謝。

嘗試使用提琴手。 http://fiddler2.com 還可以使用BodyStyle = WebMessageBodyStyle.Bare和Method =“ POST”

您是否可以在返回Json之前嘗試在服務實現中添加以下行:

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

此后,清除Firefox會話/ cookie(甚至更好的是,在“專用瀏覽模式”下打開FF),然后再次點擊該網址。

暫無
暫無

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

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