Когда я запускаю свой маленький javascript с вызовом AJAX на PHP, он всегда возвращается с помощью парсератора JSON.AJAX: Return from PHP fires error: запрошено JSON parse failed
В PHP я могу видеть, что моя JSON заполняется массив, как это: JSON кодирования: {"Year":"2012","Make":"Ford","Model":"Taurus","Error":"no error"}
This является сайт:
на основе AJAX будет вызываться, когда посетитель покидает поле VIN слева и должен автоматически заполнить три поля справа.
function decode_my_vin(myvin) {
alert("javascript began");
dataoneID = '00000';
dataoneKEY = 'x0x0x';
jQuery.ajax(
{
cache: false,
type: 'POST',
url: '/wp-content/themes/Impreza-child/vin-decoder.php',
\t dataType: "json",
data: {
\t \t \t 'value1_VIN': \t myvin,
\t \t \t 'value2_ID': \t dataoneID,
\t \t \t 'value3_KEY': \t dataoneKEY,
\t \t },
\t success: function(response) {
\t \t
\t \t \t var resultYear = response.Year;
\t \t \t var resultMake = response.Make;
\t \t \t var resultModel = response.Model;
\t \t \t var resultMessage = response.Message;
\t \t \t alert("success returned: Year " + resultYear + " Make " + resultMake + " Model " + resultModel + " Message " + resultMessage);
\t \t document.getElementById("fld_7290902_1").value = resultYear;
\t \t document.getElementById("fld_1595243_1").value = resultMake;
\t \t document.getElementById("fld_7532728_1").value = resultModel;
\t \t document.getElementById("fld_7532728_1").value = resultMessage;
\t \t \t return;
\t \t },
error: function (jqXHR, exception) {
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
\t \t alert("Error in jquery: " + msg);
$('#post').html(msg);
\t },
\t complete: function(value) {
\t \t \t alert("returned after complete: " + value);
\t \t }
\t });
}
<?php
header('Content-Type: application/json');
$vinResult = array(
'Year' => '2012',
'Make' => 'Ford',
'Model' => 'Taurus',
'Error' => 'No Error'
\t);
echo json_encode($vinResult);
?>
Поместите свой скрипт в 'document.ready' функции и связать файл JS после JQuery. Потому что, когда я проверяю ваш сайт, я обнаружил ошибку '$' не определен и не был отправлен запрос ajax. – Harish
Хм .. приятно, но каков ваш вопрос? – Viney
@Novice: Вопрос в том, будет ли правильный json, который был создан в php, не отображаться в части успеха моего вызова AJAX и почему он запускает часть ошибки с помощью json parsererror. И последнее, но не менее важное: что я могу сделать, чтобы исправить эту проблему. – nicecap