首页 > 解决方案 > Json格式错误,来自php文件的request_ajax()

问题描述

晚上好,我对 ajax 一步一步的 json 有问题,发生了什么,是我正在发送一个 json,以便用 js 动态呈现 html 元素,以同样的方式执行此步骤,新奇之处在于显然,php 代码的通知、大空白和危险正在作为我的 json 的一部分发生,而且我个人已经尝试了两天来解决它,但我没有成功,请如果有人知道任何方法可以通过只是 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) {

}
 ?>

代码js:

function createRegister(){
  var data= new FormData(form);
  for([key,value] of data.entries()){
    console.log(key+": "+value);
  }
  var xhr= new XMLHttpRequest();
  xhr.open('POST', action, true);
  xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  xhr.onreadystatechange=function(){
    if (xhr.readyState == 4 && xhr.status == 200) {
      var result= xhr.response;
      console.log("Resultado: "+ result );
      var json= JSON.parse(result);
      if(json.result==true){
        div_create(json.name);
        template(json.name, json.number, json.id_register);
        totalRegistros.innerHTML=parseInt(totalRegistros.textContent)+1;
      }

    }
  }
  xhr.send(data);

}

它带来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)

标签: javascriptphpjsonajaxhtml

解决方案


推荐阅读