この質問に対する答えを見つけました。解決しました。
説明: SimpleXMLRPCServer の上にある一連の methodCalls である Python API にアクセスしています。サーバーは、ブラウザーの GET 要求に html ページ「web_interface.html」で応答します。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」が表示されますが、テキスト ダイアログと応答ダイアログは null です。
| | Mozilla インスペクターのデータとヘッダー |
生データの投稿:
<?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 が XMLRPC サーバーに何を送信しているかを正確に確認するために、netcat ソケットを指定しました。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 要求の両方に同じホスト、同じポート、同じディレクトリを使用しています。
私は何が欠けていますか?