未调用AJAX处理程序(不确定是否发生PHP或JavaScript问题)

时间:2013-06-24 22:36:44

标签: php javascript jquery xml curl

我对这里出错的地方感到非常难过。我正在对一个接受XML的PHP​​文件进行AJAX调用,然后根据该XML生成结果并回应它。但是,不调用AJAX处理程序。下面是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));
?>

1 个答案:

答案 0 :(得分:2)

您正在从PHP脚本传回JSON:

$(function() {
    var xml = '<thisiswhere><myxmlis></myxmlis></thisiswhere>';

    $.post("http://thedomain.co.uk/sendxml.php", { xmlData: xml }, function(data) {
        alert(data);
    }, "json");
});

这意味着您要发布到同一个域。