drudfe0446838 2018-08-15 13:51 采纳率: 0%
浏览 67

我的json带有来自ajax的奇怪符号

Good evening, I have a problem with the json step by ajax, what happens, is that I am sending a json, so that dynamic rendering of html elements with js, came doing the step of this in the same way, the novelty is that apparently the notifications, large blanks, and dangers of the php code is happening as part of my json, and personally I have been trying for two days to solve it and I do not succeed, please if anyone knows any way to pass Just json what I want, please appreciate your help, this is the code:

code php create.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"]);
ini_set('precision', 17);
$newfilename = (microtime(true) . '.' . end($temp));
$image1=(((float)$newfilename) + 1.3) * 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"]);
ini_set('precision', 17);
$newfilename1 = microtime(true) . '.' . end($temp1);
$image2=((float)$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) {

}
     ?>

function js with ajax.

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.responseText;
      console.log("Resultado: "+ result );
      var json= JSON.parse(result);
      if(json.result==true){
        div_create(json.name);
        template(json.name,json.biography,json.birthDate, json.deathDate,json.id_register);
        totalRegistros.innerHTML=parseInt(totalRegistros.textContent)+1;
      }

    }
  }
  xhr.send(data);

}

Error, from console of navegator.

name: test2
main.js:151 image_principal: [object File]
main.js:151 biography: xxxxxxxxxxxxxxxxxxxxxxxx
main.js:151 birthdate: 2018-08-18
main.js:151 deathdate: 2018-08-25
main.js:151 image_biography: [object File]
main.js:159 Resultado: 




 ?>

{"result":true,"name":"test2","biography":"xxxxxxxxxxxxxxxxxxxxxxxx","birthDate":"2018-08-18","deathDate":"2018-08-25","id_register":31}
VM157:6 Uncaught SyntaxError: Unexpected token ? in JSON at position 11
    at JSON.parse (<anonymous>)
    at XMLHttpRequest.xhr.onreadystatechange (main.js:160)

what I do not understand is because the json comes with so many spaces and the symbol '?>' if I'm giving it the bd data structure, this is the first time it happens to me, and I do not understand that ajax is supposed to clean by default memory, so I do not think that's it, personally I'm lost.

  • 写回答

1条回答 默认 最新

  • douyu0725 2018-08-15 14:41
    关注

    I think for quick fix just wrap your encoded json with die() instead of echo

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分