JQuery Ajax POST -请求体不显示在原始HTTP请求
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显示原始数据如下:
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调用是否是处理这类请求的最佳方法。任何帮助将不胜感激!
谢谢!
查看responseText
success: function (response, status, xhr) {
console.log(xhr.responseText);
alert('Success Auth Token:' + response);
}
您可能会在那里看到正确的文本,但是responseXml将为空,因为文档无效。
为什么?因为你漏掉了
这行<?xml version="1.0" encoding="ISO-8859-1"?>
这将使它成为一个有效的XML文档
相关文章:
- 在等待ajax请求时显示微调器并禁用页面
- 显示IIS上javascript文件(SOAP请求)的XML响应
- 如何显示从Ajax请求返回的数组.使用Yii2
- PHP处理来自一个页面的http请求,并将其显示在另一个页面上
- 如何在发送ajax请求时显示进度加载程序
- 如何在ajax请求期间显示进度条(jquery.php)
- Ember-解决所有请求时显示列表
- 使 ajax 请求和成功数据显示在 id <选择>选择>部分中
- 为什么在页面上单击的先前链接的文本显示在 AJAX 请求中
- 正确显示从 ajax 请求到 html 的结果
- JSON请求显示控制台问题
- 如何在rails中通过JS请求显示twitter引导模式
- AJAX JSON请求显示为未定义的类型
- 跨域请求显示数据结果
- 节点- Post请求显示未定义
- 带有Angular的JSON请求显示一个空的响应
- 多选ajax请求显示错误以及如何从多选框中获取所选值
- PhantomJS get请求显示静态页面,而不是完全加载的页面
- 表单POST提交请求显示为get请求
- JQuery AJAX请求显示AJAX和PHP错误