我有这个让我回复的ajax请求:
语法输入ajax的意外结束错误
$.ajax({
url: "file.php",
type: 'POST',
dataType: "json",
data: {
string : name
},
success: function(data, textStatus, xhr) {
toastr.success(data, "Ok");
},
error: function(xhr, textStatus, errorThrown) {
toastr.error("Error: " + xhr + " error thrown: " + errorThrown+ " error " + errorThrown, "Error");
}
});
file.php 就是这个:
$string_to_found = $_POST['string'];
$file_xml = "../file.xml";
if (!file_exists($file_xml)) {
echo json_encode(array("response" => "no_file", "message"=>"File Not Found"));
} else {
$xml = simplexml_load_file($file_xml );
$number = count($xml);
for ($num = 0; $num < $number; $num++) {
$row++;
$filearray[] = array(
"cod" => (string)$xml->record[$num]->value,
"com" => (string)$xml->record[$num]->name,
);
}
foreach($filearray as $item){
if($item['com'] == $string_to_found ) {
echo json_encode(array("response" => "ok", "message"=>"Code Found", "code" => $item['cod']));
}
}
}
file.php给了我正确的值:
{"response":"ok","message":"Code Found","cod":"AA019"}
我如何解决这个问题?我试图用echo改变php print,也改变dataType和contentType