【问题标题】:Cannot get all POST Success xml data无法获取所有 POST Success xml 数据
【发布时间】:2013-01-04 17:33:18
【问题描述】:

我正在尝试获取我的 xml 标记结果并将它们加载到地图上,但是当我发出警报时,我在变量中得到的发布结果是这样的:

加载的数据:[object XMLDocument]

这是我的代码的样子:

function SendData() {

//get data from inputs

            $.ajax({
                type: "POST",
                url: "MapSearchxml.php",
                data: {
                    dataFromDate: FromDate,
                    //some more data
                    dataHasPatio: HasPatio
                },
                beforeSend: function (html) { // this happens before actual call
                },
                success: function (html) { // this happens after we get results

                alert("Data Loaded: " + html);

                }
            });
}

在 Firebug 中,发布响应如下所示:

<markers><marker id="1" lat="48.153938" lng="17.108459" /></markers>

我尝试将数据类型设置为 xml,如下所示:

                success: function (html) { // this happens after we get results
                alert("Data Loaded: " + html);
                }, 
                dataType: 'xml'
            });
}

但是没有发生任何变化。

这是我的 xml php 代码:

$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}


header("Content-type: text/xml");

echo '<markers>';

while ($row = @mysql_fetch_assoc($result)){
  echo '<marker ';
  echo 'id="' . parseToXML($row['ID']) . '" ';
  echo 'lat="' . parseToXML($row['LAT']) . '" ';
  echo 'lng="' . parseToXML($row['LNG']) . '" ';
  echo '/>';
}

echo '</markers>';

【问题讨论】:

  • 您期望返回什么?你也可以展示你的 PHP 代码吗?
  • 嘿 mccannf,我希望这整件事都能被退回 &lt;markers&gt;&lt;marker id="1" lat="48.153938" lng="17.108459" /&gt;&lt;/markers&gt;。我将使用 php 代码编辑我的帖子。

标签: jquery ajax


【解决方案1】:

这是浏览器在将 XML 对象附加到文本字符串并在警报中显示数据时处理它们的方式。您的 ajax 调用看起来可以正常工作。

你可以尝试改变:

    success: function (html) { // this happens after we get results
       alert("Data Loaded: " + html);
    }, 

只是:

    success: function (html) { // this happens after we get results
       console.log(html);
    }, 

并在 JavaScript 控制台中查看对象。 或:

    success: function (html) { // this happens after we get results
        var xmlString = (new XMLSerializer()).serializeToString(html);
        alert("Data loaded: " + xmlString);
    }, 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-18
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多