Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.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格式错误,来自请求为\u ajax()的php文件_Javascript_Php_Json_Ajax_Html - Fatal编程技术网

Javascript Json格式错误,来自请求为\u ajax()的php文件

Javascript Json格式错误,来自请求为\u ajax()的php文件,javascript,php,json,ajax,html,Javascript,Php,Json,Ajax,Html,晚上好,我对ajax的json步骤有一个问题,发生的事情是,我正在发送一个json,因此使用js动态呈现html元素,以同样的方式来完成这一步骤,新奇之处在于,显然php代码的通知、大空白和危险正在作为我的json的一部分发生,就我个人而言,我已经尝试了两天来解决它,但我没有成功,请如果有人知道任何方法来传递我想要的内容,请感谢您的帮助,以下是代码: php代码 <?php header('Content-Type: application/json; charset=utf-8'

晚上好,我对ajax的json步骤有一个问题,发生的事情是,我正在发送一个json,因此使用js动态呈现html元素,以同样的方式来完成这一步骤,新奇之处在于,显然php代码的通知、大空白和危险正在作为我的json的一部分发生,就我个人而言,我已经尝试了两天来解决它,但我没有成功,请如果有人知道任何方法来传递我想要的内容,请感谢您的帮助,以下是代码:

php代码

    <?php
header('Content-Type: application/json; charset=utf-8');

function request_ajax(){
  return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
}

$directorio=" /../../../images/artist/carpeta";

$temp = explode(".", $_FILES["image_principal"]["name"]);
$newfilename = microtime(true) . '.' . end($temp);
$image1=($newfilename + 1) * mt_getrandmax();


if(move_uploaded_file($_FILES['image_principal']['tmp_name'], __DIR__ . $directorio . $image1. $_FILES["image_principal"]["name"])){
         $imagen_urlprincipal= 'artist/'.$image1 . $_FILES["image_principal"]["name"] ;
       }

$temp1 = explode(".", $_FILES["image_biography"]["name"]);
$newfilename1 = microtime(true) . '.' . end($temp1);
$image2=$newfilename1 * mt_getrandmax();

if(move_uploaded_file($_FILES['image_biography']['tmp_name'], __DIR__ . $directorio . $image2. $_FILES["image_biography"]["name"])){
         $imagen_urlbiography= 'artist/'.$image2 . $_FILES["image_biography"]["name"] ;
       }


$name=htmlspecialchars($_POST['name']);
$bio=htmlspecialchars($_POST['biography']);
$birt=htmlspecialchars($_POST['birthdate']);
$deat=htmlspecialchars($_POST['deathdate']);


try {
  require_once('../../functions/conection.php');
  $statement="insert into `artist` values ( null , '{$name}' , '{$imagen_urlprincipal}' , '{$bio}' , '{$birt}' ,'{$deat}', '{$imagen_urlbiography}');";
  $result= $conection->query($statement);


  if (request_ajax()) {
    echo json_encode(array(
      'result'=>$result,
      'name'=> $name,
      'biography'=> $bio,
      'birthDate'=> $birt,
      'deathDate'=> $deat,
      'id_register'=> $conection->insert_id
    ));
  }else {
    exit;
  }
} catch (Exception $e) {

}
 ?>
它带来json的方式,以及控制台的相应错误:

name: test2
main.js:151 image_principal: [object File]
main.js:151 biography: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
main.js:151 birthdate: 2018-08-09
main.js:151 deathdate: 2018-08-17
main.js:151 image_biography: [object File]
main.js:159 Resultado: <br />
<b>Notice</b>:  A non well formed numeric value encountered in <b>C:\xampp\htdocs\DOM\secciones\artistas_bcv\crud\create.php</b> on line <b>12</b><br />
<br />
<b>Notice</b>:  A non well formed numeric value encountered in <b>C:\xampp\htdocs\DOM\secciones\artistas_bcv\crud\create.php</b> on line <b>21</b><br />





 ?>

{"result":true,"name":"test2","biography":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","birthDate":"2018-08-09","deathDate":"2018-08-17","id_register":29}
VM98:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at XMLHttpRequest.xhr.onreadystatechange (main.js:160)
name:test2
main.js:151 image\u principal:[对象文件]
main.js:151传记:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
main.js:151出生日期:2018-08-09
main.js:151死亡日期:2018-08-17
main.js:151 image\u传记:[对象文件]
main.js:159 Resultado:
注意:在第12行的C:\xampp\htdocs\DOM\secciones\artistas\u bcv\crud\create.php中遇到格式不正确的数值

注意:在第21行的C:\xampp\htdocs\DOM\secciones\artistas\u bcv\crud\create.php中遇到格式不正确的数值
?> {“结果”:真,“姓名”:“测试2”,“传记”:“XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”,“出生日期”:“2018-08-09”,“死亡日期”:“2018-08-17”,“身份证登记簿”:29} VM98:1未捕获的语法错误:JSON中位置0处的意外标记< 在JSON.parse()处 位于XMLHttpRequest.xhr.onreadystatechange(main.js:160)
PHP代码中出现错误,导致抛出通知:“注意:在第12行的C:\xampp\htdocs\DOM\secciones\artistas\u bcv\crud\create.PHP中遇到格式不正确的数值”,这会打乱json响应。您的
create.php
-文件的第12行是什么?我猜是
image1=($newfilename+1)*mt_getrandmax()抛出该通知。问题是
$newfilename
不包含有效的数字,因此对其进行添加
+1
将抛出该通知。执行
var\u转储($newfilename)
并检查它实际包含的内容。它应该类似于
123456.1234。what ever end($temp)-返回
。你不能有超过一个小数点。我已经删除了这一行,但在任何情况下都会发送很多空格,最后发送json,令人恼火的是xhr.response作为json的一部分发送空格。请确保你的代码中没有输出,并且在
之前没有空格/新行。顺便说一句,你对SQL注入非常开放。您应该使用预先准备好的语句,而不是像这样直接将用户数据注入到查询中。
name: test2
main.js:151 image_principal: [object File]
main.js:151 biography: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
main.js:151 birthdate: 2018-08-09
main.js:151 deathdate: 2018-08-17
main.js:151 image_biography: [object File]
main.js:159 Resultado: <br />
<b>Notice</b>:  A non well formed numeric value encountered in <b>C:\xampp\htdocs\DOM\secciones\artistas_bcv\crud\create.php</b> on line <b>12</b><br />
<br />
<b>Notice</b>:  A non well formed numeric value encountered in <b>C:\xampp\htdocs\DOM\secciones\artistas_bcv\crud\create.php</b> on line <b>21</b><br />





 ?>

{"result":true,"name":"test2","biography":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","birthDate":"2018-08-09","deathDate":"2018-08-17","id_register":29}
VM98:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at XMLHttpRequest.xhr.onreadystatechange (main.js:160)