【发布时间】:2017-04-26 04:41:09
【问题描述】:
我找到了这个问题的答案。解决了。
叙述:我正在访问一个 Python API,即 SimpleXMLRPCServer 之上的一组方法调用。服务器使用 html 页面“web_interface.html”响应浏览器 GET 请求。 HTML 页面是一个非常简单的脚本,它将 xml 参数的 XHR POST 请求发送到 XMLRPC 服务器。服务器使用标题但空文档响应 XHR POST。服务器以正确的数据响应 cURL。为什么 JavaScript 没有从服务器获取任何可读数据作为响应?
| web_interface.html |
<!DOCTYPE html>
<html>
<head>
<script>
var xrequest = '<?xml version="1.0?"><methodCall><methodName>helloWorld<methodName><params><param><firstWord><string>hello</string><firstWord></param><param><secondWord><string>world</string></secondWord></param></params></methodCall>';
function hello() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == XMLHttpRequest.DONE) {
alert(this.responseText);
alert(this.status);
alert(this.response);
}
}
xhr.open('POST', '/', true);
xhr.setRequestHeader("Authorization", "Basic " + "aGVsbG8=" + ":" + "dGVzdA==");
xhr.send(xrequest);
}
</script>
</head>
<body>
<div>
<h2 id="msgoutput">HelloWorld API Test</h2>
<button type="button" onclick="hello(); return false;">SAY HELLO!</button>
</div>
</body>
</html>
注意:单击按钮会生成警报对话框。状态对话框显示“200”,而文本和响应对话框为空。
| Mozilla Inspector 数据和标题 |
POST 原始数据:
<?xml version="1.0?"><methodCall><methodName>helloWorld<methodName><params><param><firstWord><string>hello</string><firstWord></param><param><secondWord><string>world</string></secondWord></param></params></methodCall>
响应标头:
Content-Length 332
Content-Type text/html
Date Sat, 10 Dec 2016 23:51:21 GMT
Server BaseHTTP/0.3 Python/2.7.12
请求标头:
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
AuthorizationBasic aGVsbG8=:dGVzdA== (not my actual creds, swapped fakes)
Connection keep-alive
Content-Length 218
Content-Type text/plain;charset=UTF-8
DNT1
Hostlocalhost:8442
Referer http://localhost:8442/
User-Agent Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0
|用 cURL 测试 |
:~$ curl -i --data '<?xml version="1.0"?><methodCall><methodName>helloWorld</methodName><params><param><firstWord><string>hello</string></firstWord></param><param><secondWord><string>world</string></secondWord></param></params></methodCall>' http://username:password@localhost:8442
HTTP/1.0 200 OK
Server: BaseHTTP/0.3 Python/2.7.12
Date: Sun, 11 Dec 2016 00:00:13 GMT
Content-type: text/html
Content-length: 137
<?xml version='1.0'?>
<methodResponse>
<params>
<param>
<value><string>hello-world</string></value>
</param>
</params>
</methodResponse>
注意:没问题,cURL 以文本形式返回 XML 响应。我将 cURL 指向一个 netcat 套接字,以查看它发送到 XMLRPC 服务器的确切内容。这是 cURL 命中时 netcat 显示的内容:
| cURL POST 数据 |
POST / HTTP/1.1
Host: localhost:8442
Authorization: Basic YWRtaW46Z2liYmVyc2g=
User-Agent: curl/7.47.0
Accept: */*
Content-Length: 220
Content-Type: application/x-www-form-urlencoded
<?xml version="1.0"?><methodCall><methodName>helloWorld</methodName><params><param><firstWord><string>hello</string></firstWord></param><param><secondWord><string>world</string></secondWord></param></params></methodCall>
这不是 CORS。已经在同一台机器上使用相同的浏览器测试了对 xhr.responseText 的 GET 请求。安装程序对 GET 页面和 XHR POST XMLRPC 请求使用相同的主机、相同的端口、相同的目录。
我错过了什么?
【问题讨论】:
-
so .. 基本上问题是 GET 有效,而 POST 无效 - curl 是 GET 还是 POST?
-
cURL 命令同时做这两个。 GET (URL) user:pass 像 GET 一样发送,但转换为身份验证标头。 XML 参数通过 --data 开关作为 POST 数据发送。发布作品;这是无效的响应。 Inspector 显示浏览器正在发送 POST 数据,服务器正在发送响应,其中包含 JavaScript 报告的空 XML。
-
你是说显示的 curl 命令发出两个请求?
-
@JaromandaX cURL 采用 get URL 参数并将用户名和密码转换为基本身份验证标头。据我所知,它只发出一个请求。我刚刚编辑了问题并粘贴了 cURL 发送的数据,以便您查看标题。
-
Authorization: Basic YWRtaW46Z2liYmVyc2g=vsAuthorizationBasic aGVsbG8=:dGVzdA==- 完全不同的标题
标签: javascript python xmlhttprequest xml-rpc simplexmlrpcserver