【发布时间】:2014-04-27 04:35:54
【问题描述】:
我需要在 JavaScript 中创建一个 Web 服务器客户端,但在定义请求标头时遇到了一些问题。
我需要 POST 方法和 Content-Type: "application/json"。
我试过了:
$.ajax({
url: 'http://MyWebServiceAddress',
data: JSON.stringify({user:'user',pass:'pass'}),
type: 'POST',
crossDomain: true,
dataType: 'json',
success: function () {
alert("success")
},
error: function (xhr, ajaxOptions, thrownError) {
alert("Error: " + xhr.status + "\n" +
"Message: " + xhr.statusText + "\n" +
"Response: " + xhr.responseText + "\n" + thrownError);
}
});
但是如果我把 contentType 写成这样:
contentType: '应用程序/json; charset=utf-8',
然后使用 Chrome 开发工具查看请求,我可以看到该方法已更改为“OPTIONS”并键入“text/plain”
有人可以帮助我吗?我不必使用 Ajax,所以如果有人知道一个好的 JavaScript 库来使客户端更容易,也许会导致我的问题
【问题讨论】:
标签: javascript jquery ajax web-services http-headers