我很困惑我哪里出了问题。我正在对一个接受XML的PHP文件进行AJAX调用,然后基于该XML生成一个结果并对其进行回显。下面是JavaScript和PHP。
感谢
JavaScript
$(function() {
$.post("http://thedomain.co.uk/sendxml.php", { xmlData: '
<thisiswhere>
<myxmlis>
</myxmlis>
</thisiswhere>
' }, function(data) {alert(data)}, "xml");
});
PHP
<?php
$xml_builder = $_POST['xmlData'];
// We send XML via CURL using POST with a http header of text/xml.
$ch = curl_init('http://user:pass@myserver.com/api');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_builder);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.mydomain.co.uk');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ch_result = curl_exec($ch);
curl_close($ch);
echo json_encode(htmlentities($ch_result));
?>
您正在从PHP脚本返回JSON:
$(function() {
var xml = '<thisiswhere><myxmlis></myxmlis></thisiswhere>';
$.post("http://thedomain.co.uk/sendxml.php", { xmlData: xml }, function(data) {
alert(data);
}, "json");
});
这意味着你在同一个域发帖。