我在这里看到关于类似主题的各种帖子。不幸的是,这些都没有帮助。
我对一个 php 文件进行 ajax 调用,该文件创建一个 XML 文件,假设找到数据,我从中填充适当的表单字段。
如果我将返回数据指定为 XML,如下所示,我不会成功,并且会显示警报“无法检索 XML 文件”。
function check_Address(){
alert("username=" + $("#username").attr("value"));
$.ajax({
type: "POST",
url: "admin_Check_Address.php",
data: "username=" + $("#username").attr("value"),
dataType: "xml",
success: function(return_Data){
set_Address(return_Data);
},
error: function(return_Data){
alert("Could not retrieve XML file.");
}
}); // end ajax call
}
但是,如果我删除数据类型,我会成功执行 set_Address 函数,并找到 return_Data 中的 tagNames 并且正确填充该字段。IE
function check_Address(){
alert("username=" + $("#username").attr("value"));
$.ajax({
type: "POST",
url: "admin_Check_Address.php",
data: "username=" + $("#username").attr("value"),
success: function(return_Data){
set_Address(return_Data);
},
error: function(return_Data){
alert("Could not retrieve XML file.");
}
}); // end ajax call
}
这是php:
<?php
/*
* Accept username from Ajax
* Get appropriate key
* Check for pre existing address(es)
*/
if(isset($_POST["username"])){
// retrieve user key
$sql = "SELECT * FROM User ";
$sql .= "WHERE username = ";
$sql .= "'";
$sql .= $_POST['username'];
$sql .= "'";
$sql .= " LIMIT 1;";
$user = User::find_By_Sql($sql);
$user_Key = $user[0]->get_User_Key();
// check for an existing address or addresses
$sql = "SELECT * FROM Address ";
$sql .= "WHERE user_Key = ";
$sql .= "'";
$sql .= $user_Key;
$sql .= "';";
$addresses = Address::find_By_Sql($sql);
// how many addresses found
// if none nothing to do except set focus on address 1
// if 1 address returned check active and return xml to complete fields
// if >1 return xml to create list for user to select from
echo "addr ct:" . count($addresses);
if (count($addresses) == 1){
construct_XML($addresses[0]);
}
} else {
echo "error nothing passed";
}
function construct_XML($address){
echo ("start xml");
$xml_String = "<?xml vesrion=\"1.0\"?>";
$xml_String .= "<addresses>";
$xml_String .= "<address>";
$xml_String .= "<username>";
$xml_String .= $_POST['username'];
$xml_String .= "</username>";
$xml_String .= "<address1>";
$xml_String .= $address->address_1;
$xml_String .= "</address1>";
$xml_String .= "<address2>";
$xml_String .= $address->address_2;
$xml_String .= "</address2>";
$xml_String .= "<city>";
$xml_String .= $address->city;
$xml_String .= "</city>";
$xml_String .= "<county>";
$xml_String .= $address->county;
$xml_String .= "</county>";
$xml_String .= "<country>";
$xml_String .= $address->country;
$xml_String .= "</country>";
$xml_String .= "<post_Code>";
$xml_String .= $address->post_Code;
$xml_String .= "</post_Code>";
$xml_String .= "</address>";
$xml_String .= "</addresses>";
echo $xml_String;
}
?>
读取 XML 的设置地址函数为:
function set_Address(return_Data){
$("#address_1").attr("value", $("address1", return_Data).text());
$("#address_2").attr("value", $("address2", return_Data).text());
$("#city").attr("value", $("city", return_Data).text());
$("#county").attr("value", $("county", return_Data).text());
$("#country").attr("value", $("country", return_Data).text());
$("#code").attr("value", $("post_Code", return_Data).text());
}
我在想我应该返回一个 file.xml 而不是回显字符串,但我不知道该怎么做。
有什么想法我哪里出错了吗?我可以简单地省略 dataType 并且一切正常,但我想知道为什么它不会将其识别为 XML,所以我下次了解。
非常感谢。
好的,非常感谢您的回复。是的,我在原始帖子中留下了两个错误,这些错误与 2 条不需要的行相呼应。
我现在明白我需要在字符串之前包含标题,以便让 ajax 将其作为 XML 接收。
header ("Content-Type:text/xml");
但是我仍然无法让 Jquery 正确解析从 PHP 返回的数据遇到过:
xml解析错误找不到元素位置moz-nullprincipal
我一直无法解决这个问题。因此,我更新了 ajax,以便将一个调用嵌套在另一个调用中,以使 php 返回 XML。我不确定这是否合适,但它有效:.....
这是php
<?php require_once("../../_includes/initialize.php"); ?>
<?php
/*
* Accept username from Ajax
* Get appropriate key
* Check for pre existing address(es)
*/
$tmpuser = "john@hotmail.co.uk";
if(isset($_POST["username"])){
// retrieve user key
$sql = "SELECT * FROM User ";
$sql .= "WHERE username = ";
$sql .= "'";
$sql .= $_POST['username'];
$sql .= "'";
$sql .= " LIMIT 1;";
$user = User::find_By_Sql($sql);
$user_Key = $user[0]->get_User_Key();
// check for an existing address or addresses
$sql = "SELECT * FROM Address ";
$sql .= "WHERE user_Key = ";
$sql .= "'";
$sql .= $user_Key;
$sql .= "';";
$addresses = Address::find_By_Sql($sql);
// how many addresses found
// if none nothing to do except set focus on address 1
// if 1 address returned check active and return xml to complete fields
// if >1 return xml to create list for user to select from
if (count($addresses) == 1){
construct_XML($addresses[0]);
}
} else {
//"error nothing passed";
}
function construct_XML($address){
// initialize xml object
$xml_Data = simplexml_load_string("<?xml version='1.0'?>\n<addresses></addresses>");
$address_Detail = $xml_Data->addChild("address");
$address_Detail->addChild("username", $_POST['username']);
$address_Detail->addChild("address1", $address->address_1);
$address_Detail->addChild("address2", $address->address_2);
$address_Detail->addChild("city", $address->city);
$address_Detail->addChild("county", $address->county);
$address_Detail->addChild("country", $address->country);
$address_Detail->addChild("post_Code", $address->post_Code);
$fp = fopen("addresses.xml","wb");
fwrite($fp,$xml_Data->asXML());
fclose($fp);
}
?>
这是修改后的 Ajax 调用:
function check_Address(){
$.ajax({
type: "POST",
url: "admin_Check_Address.php",
data: "username=" + $("#username").attr("value"),
success: function(return_Data){
$.ajax({
type: "POST",
url: "addresses.xml",
dataType: "xml",
success: function(xml_Data){
set_Address(xml_Data);
},
error: function(){
alert("Could not retrieve XML file.");
}
}); // end innerajax call
},
error: function(){
alert("Could not retrieve XML file.");
}
}); // end ajax call
}
调整 set_Address 函数
function set_Address(xml_Data){
$("#address_1").attr("value", $("address1", xml_Data).text());
$("#address_2").attr("value", $("address2", xml_Data).text());
$("#city").attr("value", $("city", xml_Data).text());
$("#county").attr("value", $("county", xml_Data).text());
$("#country").attr("value", $("country", xml_Data).text());
$("#code").attr("value", $("post_Code", xml_Data).text());
}
所以不是 100% 我真的希望能够一击完成 ajax 调用。因此,非常欢迎任何进一步的建议。