[英]JQuery Ajax POST - Request Body not showing up in Raw HTTP Request
我有一個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將Raw數據顯示為:
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調用是否是處理此類Request的最佳方法。 任何幫助,將不勝感激!
謝謝!
看看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文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.