JQuery Ajax POST - 请求体未显示在原始HTTP请求中

3

我有一个REST服务API,我试图从Javascript访问它,其中包括GET和POST HTTP请求,但我很难从JavaScript中获取包含请求主体的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>

当我执行此操作时,Fiddler显示以下原始数据:
POST http://api.mydomain.com/xml/accounts/authenticate?api_key=12345678-1234-1234-1234-123456789012 HTTP/1.1
Host: api.mydomain.com
content-type: text/xml
Content-Length: 143

<Authentication xmlns="http://schemas.mydomain.com/authentication">
 <Firstname>Joe</Firstname>
 <Lastname>Blow</Lastname>
</Authentication>

这里是我尝试使用Jquery Ajax调用完成同样请求的最佳方案。
function accessArctos() {
$.ajax({
    url: 'http://api.mydomain.com/xml/accounts/authenticate?api_key=12345678-1234-1234-1234-123456789012',
    type:"POST",
    data: '<Authentication xmlns="http://schemas.mydomain.com/authentication"><Firstname>Joe</Firstname><Lastname>Blow</Lastname></Authentication>',
    error: function() { alert("No data found."); },
    contentType: "text/xml",
    success: function (response) {
        alert('Success Auth Token:' + response);
    }
});
}

当我执行这段代码时,Fiddler显示原始数据如下:
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

我不确定为什么请求正文没有显示出来。我不知道使用Jquery ajax调用是否是处理此类请求的最佳方法。任何帮助将不胜感激!谢谢!

我发现的一个问题是,在服务器端,它没有处理在POST之前发送的OPTIONS请求。我在ASP.NET服务器的global.aspx中添加了一个Application_BeginRequest()方法。现在POST失败了,但是在我的Fiddler原始数据中出现了一个XML记录作为请求正文。我将继续调试,但至少似乎已经解决了我的最初的问题。 - user2455846
protected void Application_BeginRequest(object sender, EventArgs e) { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");if (HttpContext.Current.Request.HttpMethod == "OPTIONS") { //headers are handling the "pre-flight" OPTIONS call sent by the browser HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept"); HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000"); HttpContext.Current.Response.End();}} - user2455846
1个回答

0

查看responseText

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文档


我将 <?xml 添加到字符串中,仍然收到相同的响应。数据:'<?xml version="1.0" encoding="ISO-8859-1"?><Authentication xmlns="http://schemas.mydomain.com/authentication"><Firstname>Joe</Firstname><Lastname>Blow</Lastname></Authentication>'。由于似乎没有发送正文并进入错误,我的请求返回一个 405 未经授权。 - user2455846
所以,405意味着您的服务器端出现了问题。 - epascarello

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接