[英]“SyntaxError: JSON.parse: unexpected character” Error when passing multiple variables from AJAX to PHP
I am using AJAX to pass variables from a form to a PHP page to process data at a database. 我正在使用AJAX将变量从表单传递到PHP页面,以处理数据库中的数据。
Once the user clicks a button it fires a the following JavaScript: 用户单击按钮后,将触发以下JavaScript:
$(document).ready(function() {
$("#myForm").submit(function(event) {
/* validate the fields */
var firstDate= "11/10/2014"
var secondDate = "10/10/2014"
var myString = "some Text";
var myArray = ["name1", "name2", "name3", "123-123-33gf"];
processIT(firstDate, secondDate, muString, myArray);
});/* end of submit */
});
function processIT(firstDate, secondDate, muString, myArray) {
var response = "";
$(function () {
$.ajax({
url: 'api.php', // the script to call to get data
type: "POST",
data: {
firstDate: firstDate,
secondDate : secondDate ,
myString : myString ,
myArray : myArray ,
}, // you can insert url argumnets here to pass to api.php
dataType: 'json', // return data format
success: function(data) { //
alert(data);
},
error: function (jqXHR, textStatus, errorThrown){
console.log(textStatus, errorThrown);
},
});
});
return response;
}
The api.php page has the following api.php页面具有以下内容
<?php
if ( isset($_POST["firstDate"]) && !empty($_POST["firstDate"])){
$response .= "<p>firstDate= " . $_POST["firstDate"] . "</p>";
}
else $response .= " 1 ";
if ( isset($_POST["secondDate"]) && !empty($_POST["secondDate"])){
$response .= "<p>secondDate = " . $_POST["secondDate"] . "</p>";
}
else $response .= " 2 ";
if ( isset($_POST["myString"]) && !empty($_POST["myString"])){
$response .= "<p>myString = " . $_POST["myString"] . "</p>";
}
else $response .= " 3 ";
if ( isset($_POST["myArray"]) && !empty($_POST["myArray"])){
$response .= "<p>myArray = " . $_POST["myArray"] . "</p>";
}
else $response .= " 4 ";
echo json_encode($response);
?>
But when I click the button I get the following error: 但是,当我单击按钮时,出现以下错误:
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
SyntaxError:JSON.parse:JSON数据的第1行第1列出现意外字符
But if I change the POST to GET, I can see the passed variables, but still get the same error. 但是,如果我将POST更改为GET,则可以看到传递的变量,但仍然会遇到相同的错误。
Any ideas what I am doing wrong? 有什么想法我做错了吗?
Your PHP file is not outputting a valid JSON response, that's why JSON.parse
is throwing an error. 您的PHP文件未输出有效的JSON响应,这就是
JSON.parse
引发错误的原因。 There are a number of errors in your PHP code, and those errors are being included in the output, thus making an invalid JSON response. 您的PHP代码中有很多错误,并且这些错误已包含在输出中,从而使JSON响应无效。
console.log("firstDate" + $_POST["firstDate"]);
This is not valid PHP code. 这不是有效的PHP代码。 PHP doesn't have
console.log()
. PHP没有
console.log()
。 It has echo
. 它具有
echo
。 PS You use .
PS您使用
.
to concatenate strings in PHP, not +.
连接PHP中的字符串,而不是
+.
$_POST["secondDate "]
$_POST["myString "]
$_POST["myArray "]
These keys. 这些键。 There is no space at the end.
末尾没有空格。 They should be:
他们应该是:
$_POST["secondDate"]
$_POST["myString"]
$_POST["myArray"]
Finally, $_POST["myArray"]
is an array . 最后,
$_POST["myArray"]
是一个array 。 You can't concatenate it to a string. 您不能将其连接为字符串。 Try this:
尝试这个:
$response .= "<p>myArray = ".implode(', ', $_POST["myArray"])."</p>";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.