Javascript 如何删除JSON数据第1行第1列的SyntaxError:JSON.parse:意外字符
我使用表单向导类型,在该表单中我有3个步骤Javascript 如何删除JSON数据第1行第1列的SyntaxError:JSON.parse:意外字符,javascript,php,json,Javascript,Php,Json,我使用表单向导类型,在该表单中我有3个步骤 基础数据 更多信息 确认书 我填写了第一步的意思,然后单击提交(下一步)按钮,我从这里得到了值register newuser.php,如果条件是这样的话,我从我写的页面中得到了值 if (!empty($birthday && $department)) {do something} 这给了我 SyntaxError: JSON.parse: unexpected character at line 1 column 1 of th
if (!empty($birthday && $department)) {do something}
这给了我
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data**
所以我把if条件改为
if (!empty($birthday)) { do something}
这是可行的,但我还想检查$birthday
和$department
$(文档).ready(函数(){
$(“#用户提交”)。单击(函数(事件){
event.preventDefault();
var formData=new formData();
var formData=new formData($('#newUserForm')[0]);
formData.append('file',$('input[type=file]')[0].files[0]);
$.ajax({
url:'php/register newuser.php',
键入:“POST”,
数据:formData,
async:false,
cache:false,
contentType:false,
processData:false,
成功:功能(数据){
var res=jQuery.parseJSON(数据);//转换json
控制台日志(res);
},
});
});
});
//register-newuser.php
-
-
-
名字*
照片上传*
改变
选择文件
出生日*
系*
成功表单已成功提交。
-
以前的
-
下一个
问题在于AJAX调用期望返回数据的方式。你需要做两件事
首先,将标题添加到PHP,告诉客户端您正在发送JSON:
// This should be one of the first lines in the PHP file
header('Content-Type: application/json');
其次,使用$.ajax
的dataType
属性将JSON读回,如下所示:
$.ajax({
url: 'php/register-newuser.php',
type: 'POST',
data: formData,
dataType: 'json', // This tells jQuery that it is getting JSON back - it will parse it for you
async: false,
cache: false,
contentType: false,
processData: false,
success: function (data) {
// data will be parsed by jQuery into an object or array based on the JSON
console.log(data);
},
});
您可以尝试在ajax调用中添加属性
dataType:'json'
,并在成功回调中删除方法jQuery.parseJson(data)
例如:
$.ajax({
url:'php/register newuser.php',
键入:“POST”,
数据:formData,
数据类型:“json”,
async:false,
cache:false,
contentType:false,
processData:false,
成功:功能(数据){
控制台日志(数据);
}
});代码>Hai Matthew Herbst我做了你的行代码,但现在还是一样error@KaniR您在PHP中添加了标题行,在ajax调用中添加了dataType
,并删除了var res=jQuery.parseJSON(数据)代码>?获取如下错误
解析错误:语法错误,意外(&&;'(T_BOOLEAN_和),在第41行的/home/srisanka/public_html/cupid/TV/php/register-newuser.php中应为“'),是否有一个包含php和html的文件,html文件与我编写的js和PHP文件不同,我创建了一个名为PHP的文件夹,在PHP文件夹中,我编写的PHP代码名是register newuser.PHP'geting error像这样
解析错误:语法错误,意外'&&;'(T_BOOLEAN_和),在第41行的/home/srisanka/public_html/cupid/TV/php/register-newuser.php中应为“'),Hai user2069766我遇到这样的错误
解析错误:语法错误,意外”&&;'(T_BOOLEAN_和),第41行的/home/srisanka/public_html/cupid/TV/php/register-newuser.php中应为“)”