Javascript jQueryAjax请求后正文未显示在原始HTTP请求中

Javascript jQueryAjax请求后正文未显示在原始HTTP请求中,javascript,jquery,json,http,request,Javascript,Jquery,Json,Http,Request,我有一个REST服务API,我正试图从Javascript访问它,其中包括GET和POST HTTP请求,我很难从包含请求体的Javascript获取word的POST命令 我可以使用Fiddler生成POST请求并获得有效响应,但我不知道如何用Javascript编写类似的代码 Fiddler请求的示例如下: http://api.mydomain.com/xml/accounts/authenticate?api_key=12345678-1234-1234-1234-12345678901

我有一个REST服务API,我正试图从Javascript访问它,其中包括GET和POST HTTP请求,我很难从包含请求体的Javascript获取word的POST命令

我可以使用Fiddler生成POST请求并获得有效响应,但我不知道如何用Javascript编写类似的代码

Fiddler请求的示例如下:

http://api.mydomain.com/xml/accounts/authenticate?api_key=12345678-1234-1234-1234-123456789012
Request Header:
Host: api.mydomain.com
content-type: text/xml
Content-Length: 123

Request Body:
<Authentication xmlns="http://schemas.mydomain.com/authentication">
 <Firstname>Joe</Firstname>
 <Lastname>Blow</Lastname>
</Authentication>
我不知道为什么请求主体没有出现。我不知道jQueryAjax调用是否是处理此类请求的最佳方法。任何帮助都将不胜感激


谢谢

查看响应文本

success: function (response, status, xhr) {
    console.log(xhr.responseText);
    alert('Success Auth Token:' + response);
}
您可能会在那里看到正确的文本,但是responseXml将为null,因为文档无效

为什么??因为你错过了线路

<?xml version="1.0" encoding="ISO-8859-1"?>


这将使其成为有效的XML文档

我添加了So a 405表示服务器端有问题。我发现的一个问题是,在服务器端,它没有处理在发布之前首先发送的选项请求。我在ASP.NET服务器上的global.aspx中添加了一个应用程序_BeginRequest()方法。它现在在POST上失败了,但是在我的Fiddler原始数据中显示了一条XML记录作为请求主体。我将继续调试,但似乎我至少通过了我的初始问题。受保护的无效应用程序_BeginRequest(对象发送方,EventArgs e){HttpContext.Current.Response.AddHeader(“访问控制允许来源”,“*”);如果(HttpContext.Current.Request.HttpMethod==“选项”){//Header正在处理“预飞行”由浏览器HttpContext.Current.Response.AddHeader(“访问控制允许方法”、“获取、发布、放置、删除”);HttpContext.Current.Response.AddHeader(“访问控制允许头”、“内容类型、接受”);HttpContext.Current.Response.AddHeader(“访问控制最大年龄”、“1728000”);HttpContext.Current.Response.End();}}
OPTIONS http://api.mydomain.com/xml/accounts/authenticate?api_key=12345678-1234-1234-1234-123456789012 HTTP/1.1
Host: api.mydomain.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Origin: http://localhost:52381
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
success: function (response, status, xhr) {
    console.log(xhr.responseText);
    alert('Success Auth Token:' + response);
}
<?xml version="1.0" encoding="ISO-8859-1"?>