Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何修复';JSON分析错误:意外的标识符数组';发出AJAX请求时_Javascript_Php_Html_Ajax - Fatal编程技术网

Javascript 如何修复';JSON分析错误:意外的标识符数组';发出AJAX请求时

Javascript 如何修复';JSON分析错误:意外的标识符数组';发出AJAX请求时,javascript,php,html,ajax,Javascript,Php,Html,Ajax,我正在设置联系人表单验证,需要php脚本“sendmail.php”中的变量$msg。实际的邮件系统正在工作,脚本正在从index.php接收表单输入,并且正在工作(成功发送电子邮件)。。。 问题是‘success:function(data)’不起作用,我花了几天时间试图找出我做错了什么 我尝试更改为$.post函数,比较了许多类似的示例,使用“error:”函数进行调试 该错误显示在控制台“console.log(errorshown)”上 我使用的是Safari,我从sendmail.ph

我正在设置联系人表单验证,需要php脚本“sendmail.php”中的变量$msg。实际的邮件系统正在工作,脚本正在从index.php接收表单输入,并且正在工作(成功发送电子邮件)。。。 问题是‘success:function(data)’不起作用,我花了几天时间试图找出我做错了什么

我尝试更改为$.post函数,比较了许多类似的示例,使用“error:”函数进行调试

该错误显示在控制台“console.log(errorshown)”上

我使用的是Safari,我从sendmail.php的XHRs文件夹中获得: 输出:

//错误消息
$msg='';
如果(isset($\u POST['name'])和&isset($\u POST['email'])和&isset($\u POST['message'])){
//获取表单数据
$name=htmlspecialchars($_POST['name']);
$email=htmlspecialchars($_POST['email']);
$message=htmlspecialchars($_POST['message']);
变量转储($\u POST);
//核实
如果(!empty($name)&&!empty($email)&&!empty($message)){
//姓名检查
如果(strlen($name)=30){
$msg='请键入一个4到30个字符之间的名称';
}否则{
//电子邮件检查
如果(!filter_var($email,filter_VALIDATE_email)){
$msg='请提供有效的电子邮件';
}否则{
//消息检查
if(strlen($message)<10){
$msg='请提供一条超过10个字符的消息';
}否则{
//发送电子邮件
$msg='电子邮件已成功发送…';
echo json_encode(['code'=>200,'msg'=>$msg]);
出口
}捕获(例外$e){
$msg='错误:未发送电子邮件…';
}
}
}
}
}否则{
$msg='请填写所有字段';
}
}
echo json_encode(['code'=>404,'msg'=>$msg]);
``

检查您的代码和以下内容后:

array(3) {
  ["name"]=>
  string(12) "asfasfasfasf"
  ["email"]=>
  string(9) "asfasfasf"
  ["message"]=>
  string(0) ""
}
{"code":404,"msg":"Please fill in all fields"}
我意识到此时代码中有一个var_转储:

if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['message'])) {

    // Get Form Data
    $name = htmlspecialchars($_POST['name']);
    $email = htmlspecialchars($_POST['email']);
    $message = htmlspecialchars($_POST['message']);

    var_dump($_POST); // <---- Remove this and everything should be okay
if(isset($\u POST['name'])和&isset($\u POST['email'])和&isset($\u POST['message'])){
//获取表单数据
$name=htmlspecialchars($_POST['name']);
$email=htmlspecialchars($_POST['email']);
$message=htmlspecialchars($_POST['message']);

var_dump($_POST);//就像在
echo json_encode(['code'=>404,'msg'=>$msg]);
我很抱歉忘记了回显,但问题仍然存在!确保在回显数据之前或之后没有任何输出(包括空格、换行符等)。一个好的做法是在回显json数据后退出;
,以防止执行任何其他操作。此外,在发出请求时,请检查浏览器开发工具中的“网络”选项卡,以便查看它实际返回的内容。这可能是文件中的某个错误消息。请复制/粘贴实际代码,而不是重新创建手写一些类似的东西。你们这家伙太棒了谢谢!解决了!
array(3) {
  ["name"]=>
  string(12) "asfasfasfasf"
  ["email"]=>
  string(9) "asfasfasf"
  ["message"]=>
  string(0) ""
}
{"code":404,"msg":"Please fill in all fields"}
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['message'])) {

    // Get Form Data
    $name = htmlspecialchars($_POST['name']);
    $email = htmlspecialchars($_POST['email']);
    $message = htmlspecialchars($_POST['message']);

    var_dump($_POST); // <---- Remove this and everything should be okay