javascript - 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)
解决方案
推荐阅读
- php - 将数组组合到有组织的有效负载数据
- javascript - 每次状态更改/渲染都会触发 Data Fetch 自定义 Hook
- android - Firebase - 应用分发未能获取应用信息:[403] 调用者没有权限
- laravel - 有没有办法(模拟)加快时间(使 1 秒像 1 分钟一样过去)x60?
- javascript - 上传的 Google Drive 文件无法通过 API 更新
- postgresql - 使用规则在视图 Postgresql 上插入/更新/删除
- php - mPDF SetSourceFile - 从临时存储中导入 PDF
- javascript - Jquery在使用数组时查找数字是否在两个数字之间
- javascript - 在 webRTC 中切换摄像头
- angular - 订阅的单元测试