PHP发布到ASP表单,将XML结果返回到PHP页面。

时间:2011-07-28 15:08:11

标签: php jquery ajax asp-classic cross-domain

我必须使用的设置和我需要的设置如下:

  • 我提供了一个ASP表单(在另一个域上),在提交时以 XML 格式输出搜索结果。

  • 我正在开发一个PHP网站(我公司域

  • 从这个PHP网站我需要能够查询所述ASP表单并将XML结果发回PHP页面( on my companies domain

    < / LI>
  • 必须将变量“Client = * ”发送到ASP表单才能生效。

到目前为止我尝试了什么......

  • jQuery.ajax尝试使用此代码执行正常的帖子请求:

    $.ajax({
    url: "http://www.example.com/xml/aspfile.asp",
    crossDomain: true,
    cache: false,
    dataType: ($.browser.msie) ? "text" : "xml",
    data: { Client: "clientname" etc },
    type: 'post',
    xhrFields: {
        withCredentials: true
    },
    error: function(){
        alert("error");
    },
    success: function(data){
        var xml;
        if (typeof data == "string") {
            xml = new ActiveXObject("Microsoft.XMLDOM");
            xml.async = false;
            xml.loadXML(data);
        } else {
            xml = data;
        }
    } });
    

注意:

我已经尝试了很多不同的上述代码迭代,从基本的ajax请求到更复杂的代码,如上所述。

上面的代码返回以下错误,这是我遇到的一个错误。

XML Parsing Error: no element found Location: moz-nullprincipal:{14ce834e-ef24-43f8-b338-7202241298a5} Line Number 1, Column 1:^

我需要什么

理想情况下,某些代码可以正常工作......如果我无法获得有关如何使其发挥作用的想法或建议。

非常感谢所有那些为你的时间发布答案的人。

编辑:根据此处的要求,XML的外观如何

<quicksearchresults>
        <heading>
            <title1>River Cruises</title1>
            <title2>Quick Search</title2>
            <cruise_nos>732</cruise_nos>
            <earliest_date>01/08/11</earliest_date>
            <latest_date>01/09/11</latest_date>
            <river>Anywhere</river>
            <linename>Any</linename>
        </heading>
        <rivercruiselist>
            <holdate>28/08/11</holdate>
            <linename>The River Cruise Line</linename>
            <shipname>Esmerelda</shipname>
            <shiplink>url</shiplink>
            <cruisename>Cruise+The+Danube+to+Vienna+%26+Budapest</cruisename>
            <cruiselink>url</cruiselink>
            <river>Danube</river>
            <ratingicon>Images/BudgetIcon.png</ratingicon>
            <flyfrom>Linz</flyfrom>
            <flyback>linz</flyback>
            <cruisenights>7</cruisenights>
            <vacationdays>10</vacationdays>
            <lowprice>0</lowprice>
            <highprice>0</highprice>
            <flights>including Coach</flights>
            <soldout>Yes</soldout>
            <enquiryformlink>url</enquiryformlink>
            <enquiryformimage>Images/TravelEButton.png</enquiryformimage>
        </rivercruiselist>
</quicksearchresults>

1 个答案:

答案 0 :(得分:1)

使用您的PHP服务器作为代理:您向自己的PHP页面发出AJAX请求,该页面使用curl从外部源获取XML并将其返回给您,以便您可以解析它。

var xml;
if (typeof data == "string") {
    xml = new ActiveXObject("Microsoft.XMLDOM");
    xml.async = false;
    xml.loadXML(data);
} else {
    xml = data;
}

这部分不是必需的,您可以使用jQuery选择器来解析XML。从它的外观来看,您的请求不会返回任何结果。 alert(xml)产生什么? AFAIK你无法进行跨域POST,你可以使用一些GET黑客进行跨域JSONP

您的示例XML似乎无效,因为第一个<quicksearchresults>节点永远不会关闭。

这是一个例子,你将不得不原谅我破解的PHP,因为我有一段时间没有使用它。

// post the contents of the form to our PHP proxy

    $.post("myproxy.php", $("#myform").serialize, function(data) {
    // do something with returned xml here.

    },"xml");

代理示例(myproxy.php):

 <?php 

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://www.remotedomain.com/api.php");
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array(
            'client*' => 'something',
        ));
        $result = curl_exec($ch);
        curl_close($ch);
        echo $result;
 ?>