[英]JavaScript gets null response or empty responseXML from Python SimpleXMLRPCServer
我找到了這個問題的答案。 解決了。
敘述 :我正在訪問Python API,即SimpleXMLRPCServer頂部的一組methodCalls。 服務器使用html頁面“ web_interface.html”響應瀏覽器GET請求。 HTML頁面是一個非常簡單的腳本,它將xml參數的XHR POST請求發送到XMLRPC服務器。 服務器使用標題但空文檔來響應XHR POST。 服務器用正確的數據響應cURL。 為什么JavaScript無法從服務器獲得任何可讀數據作為響應?
<!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”,而“文本”和“響應”對話框為空。
<?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 -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顯示的內容:
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請求使用相同的主機,相同的端口,相同的目錄。
我想念什么?
Python SimpleXMLRPCServer中有一些代碼可以處理Cookie。 問題是,當瀏覽器建立連接時,代碼無法處理cookie對象。 這段代碼在每次調用時都會引發錯誤。 它正在阻止服務器將響應文本發送到瀏覽器。 在編寫Python代碼段以將調試信息輸出到文件后,我學到了這一點。 我刪除了cookie代碼,然后將響應XML成功從服務器發送到瀏覽器。
我還發現從XHR Send語句發送用戶名/密碼會引發服務器身份驗證錯誤。 服務器期望憑據以身份驗證的形式出現在POST標頭中:Basic base64 [btoa(username:password)]。
在這種情況下,內容類型標頭不是罪魁禍首。 現在,客戶端和服務器上的content-type都設置為text / xml。
這是經過修改的JavaScript代碼,可以正常工作:
<!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) {
}
}
xhr.open("POST", "/", true);
xhr.setRequestHeader("Authorization", "Basic " + btoa("admin" + ":" + "1234"));
xhr.setRequestHeader("Content-Type", "text/xml")
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.