[英]400 Bad Request HTTP Response using a WCF POST via JQuery
無法讓我的JQuery POST被WCF服務接受。 這是來自javascript的POST:
function jqueryPost() {
var url = "/LoggingTest";
$.post(url, { message: "test message" });
}
這是我通過接口接受POST的方式:
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "/LoggingTest",
BodyStyle = WebMessageBodyStyle.Bare)]
void LoggingTest(string message);
並實施:
public void LoggingTest(string message)
{
log.Debug(message, null);
}
當我調用函數jqueryPost時,我在Web檢查器中看到了400 Bad Request的HTTP響應。 不確定如何讓POST請求工作。
(在7/1上添加)
@James,這是web檢查器的輸出:
http:// localhost:4252 / LoggingTest HTTP信息
請求方法:POST
狀態代碼:400錯誤請求
請求標題
接受: /
緩存控制:最大年齡= 0
內容類型:應用/ X WWW的窗體-urlencoded
來源: http:// localhost:4252
推薦人: http:// localhost:4252 /
User-Agent:Mozilla / 5.0(Windows; U; Windows NT 5.1; C - )AppleWebKit / 532.4(KHTML,如Gecko)Qt / 4.6.2 Safari / 532.4
X-要求 - 由於:XMLHttpRequest的
表格數據
消息:測試消息
響應標題
內容長度:1165
內容類型:text / html的
日期:2010年7月1日星期四18:56:15 GMT
服務器:HTTPAPI / 1.0
所以,我剛結束這樣做,界面:
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "LoggingTest/{logID}/{logLevel}?errorCode={errorCodeInt}",
BodyStyle = WebMessageBodyStyle.Bare)]
void LoggingTest(string logID, string logLevel, int errorCodeInt, Stream message);
執行:
public void LoggingTest(string logID, string logLevel, int errorCodeInt, Stream message)
{
switch (logLevel)
{
case "error":
log.Error(errorCodeInt, message, null);
break;
case "warn":
log.Warn(errorCodeInt, message, null);
break;
case "info":
log.Info(errorCodeInt, message, null);
break;
case "debug":
log.Debug(errorCodeInt, message, null);
break;
}
}
現在它有效。 必須與UriTemplate中傳遞的參數有關,因為當我更改它以傳遞參數時,如下所示:
UriTemplate = "LoggingTest/{logID}/{logLevel}?errorCode={errorCodeInt}",
它開始接受POST。
編輯7/7:這也是最終的JavaScript:
jqueryPost('LoggingTest/LogID/debug?errorCode=0', { message: 'this is a test message'} ;
function jqueryPost(url, message) {
$.post(url, message);
}
嘗試在服務合同上添加以下行,我認為你應該使用Bare的WrappedRequest
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
看看這篇文章 ,了解更多裝飾品
它可能只是讓它為你工作的謎題的一部分,但這讓我感到有些困惑:
您可能需要仔細檢查您的JSON語法,我認為它需要是變量和變量名稱的雙引號字符串。
例如
function jqueryPost() {
var url = "/LoggingTest";
$.post(url, { message: "test message" });
}
需要是:
function jqueryPost() {
var url = "/LoggingTest";
$.post(url, { "message": "test message" });
}
nb雙引號“消息”
編輯:感謝下面的反饋,這里有一些鏈接可能對格式化JSON有用:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.