我尝试使用AJAX FormData
从JavaScript发送一些文本数据和一些文件到PHP。此外,我尝试将PHP中的数组作为响应发送到JavaScript,因此我在PHP中使用json_encode()
,在JavaScript中使用JSON.parse()
。代码似乎有效,但是AJAX向我展示了PHP在错误警报中的响应,尽管响应很好。这是我的代码:
JS:
var model = document.getElementById("model").value;
var numerfabryczny = document.getElementById("numer_fabryczny").value;
var data = document.getElementById("data_zakupu").value;
var imienazwisko = document.getElementById("imienazwisko").value;
var adres = document.getElementById("adres").value;
var tel = document.getElementById("numer_tel").value;
var email = document.getElementById("email").value;
var opis = document.getElementById("opis").value;
var zal1 = $("#zal1").get(0).files[0];
var zal2 = $("#zal2").get(0).files[0];
var zal3 = $("#zal3").get(0).files[0];
var zal = new FormData();
zal.append('rodzaj', rodzaj);
zal.append('marka', marka);
zal.append('model', model);
zal.append('kategoria', kategoria);
zal.append('numerfabryczny', numerfabryczny);
zal.append('data', data);
zal.append('adres', adres);
zal.append('imienazwisko', imienazwisko);
zal.append('tel', tel);
zal.append('email', email);
zal.append('opis', opis);
zal.append('lang', lang);
zal.append('zal1', zal1);
zal.append('zal2', zal2);
zal.append('zal3', zal3);
AJAX:
$.ajax({
type: "POST",
dataType: "json",
url: "php/sprawdz-naprawa.php",
data: zal,
processData: false,
contentType: false,
success: function(data){
var returnedData = data;
rodzajerr.style.display = 'none';
markaerr.style.display = 'none';
kategoriaerr.style.display = 'none';
modelerr.style.display = 'none';
numererr.style.display = 'none';
dataerr.style.display = 'none';
imienazwiskoerr.style.display = 'none';
adreserr.style.display = 'none';
telerr.style.display = 'none';
emailerr.style.display = 'none';
opiserr.style.display = 'none';
zal1err.style.display = 'none';
zal2err.style.display = 'none';
zal3err.style.display = 'none';
if(returnedData=="ok")
{
alert("zgloszenie wysłano");
}
else
{
if(returnedData=="error")
{
alert("wystąpił error");
}
else
{
//returnedData=eval(data);
returnedData = JSON.parse(data);
//returnedData = JSON.stringify(data);
//returnedData = JSON.parse(JSON.stringify(data));
//returnedData=$.parseJSON(data);
if(returnedData['rodzaj']!=""){rodzajerr.style.display = 'block'; rodzajerr.innerHTML =returnedData['rodzaj'];}else{rodzajerr.style.display = 'none';}
if(returnedData['marka']!=""){markaerr.style.display = 'block'; markaerr.innerHTML =returnedData['marka'];}else{markaerr.style.display = 'none';}
if(returnedData['kategoria']!=""){kategoriaerr.style.display = 'block'; kategoriaerr.innerHTML =returnedData['kategoria'];}else{kategoriaerr.style.display = 'none';}
if(returnedData['model']!=""){modelerr.style.display = 'block'; modelerr.innerHTML =returnedData['model'];}else{modelerr.style.display = 'none';}
if(returnedData['numer']!=""){numererr.style.display = 'block'; numererr.innerHTML =returnedData['numer'];}else{numererr.style.display = 'none';}
if(returnedData['data']!=""){dataerr.style.display = 'block'; dataerr.innerHTML =returnedData['data'];}else{dataerr.style.display = 'none';}
if(returnedData['imie']!=""){imienazwiskoerr.style.display = 'block'; imienazwiskoerr.innerHTML =returnedData['imie'];}else{imienazwiskoerr.style.display = 'none';}
if(returnedData['adres']!=""){adreserr.style.display = 'block'; adreserr.innerHTML =returnedData['adres'];}else{adreserr.style.display = 'none';}
if(returnedData['tel']!=""){telerr.style.display = 'block'; telerr.innerHTML =returnedData['tel']; }else{ telerr.style.display = 'none'; }
if(returnedData['email']!=""){emailerr.style.display = 'block'; emailerr.innerHTML =returnedData['email'];}else{emailerr.style.display = 'none';}
if(returnedData['opis']!=""){opiserr.style.display = 'block'; opiserr.innerHTML =returnedData['opis'];}else{opiserr.style.display = 'none';}
if(returnedData['zal1']!=""){zal1err.style.display = 'block'; zal1err.innerHTML =returnedData['zal1'];}else{zal1err.style.display = 'none';}
if(returnedData['zal2']!=""){zal2err.style.display = 'block'; zal2err.innerHTML =returnedData['zal2'];}else{zal2err.style.display = 'none';}
if(returnedData['zal3']!=""){zal3err.style.display = 'block'; zal3err.innerHTML =returnedData['zal3'];}else{zal3err.style.display = 'none';}
}
}
},
error: function(msg){
alert(msg.responseText);
}
});
PHP:
$errors = array();
$naprawa = $_POST['rodzaj']; (i will do it for all data, but its work, when i echo this it is show in alert in error, so that code see value of variable)
$errors['rodzaj']="";
if($naprawa == '---')
{$errors['rodzaj'] = $f01[$j];} (here i fill my errors array and do something like this for all variables)
填充数组的另一个例子:
$errors['tel']="";
if (empty($telefon))
{$errors['tel'] = $f14[$j];}
else if ((strlen($telefon) > 20) && (!empty($telefon)))
{$errors['tel'] = $f05[$j];}
else if ((!preg_match('/^[0-9-\s\(\)\/]{1,20}+$/', $telefon)) && (!empty($telefon)) && (strlen($telefon) < 21))
{$errors['tel'] = $f15[$j];}
填充数组工作,因为在ajax的alert中的字符串是好文本 在那里我发送回复:
echo json_encode($errors);
以下提醒我:
{"rodzaj":"Prosimy wybra\u0107 typ naprawy.","marka":"Prosimy wybra\u0107 mark\u0119.","kategoria":"Prosimy wybra\u0107 kategori\u0119 sprz\u0119tu.","model":"Prosimy wprowadzi\u0107 model.","numer":"Prosimy wprowadzi\u0107 numer fabryczny.","data":"","imie":"Prosimy wprowadzi\u0107 imi\u0119.","adres":"Prosimy wprowadzi\u0107 adres.","tel":"Prosimy wprowadzi\u0107 numer telefonu.","email":"Prosimy wprowadzi\u0107 adres email.","opis":"","zal1":"","zal2":"","zal3":""}
我们看到字段名称和值,例如:name - &gt; rodzaj;价值 - &gt; Prosimy wybra \ u0107 typ naprawy。所以回复是好的。但问题可能是在ajax中解析。 BTW。我想在div中将此值设置为文本,但是当我尝试不使用json但是datetype text / html时,我得到div的文本,如undefined。当我在浏览器的控制台中评论数据类型时,我得到:
“SyntaxError:missing; before statement”
有人看到有些缺陷吗?
答案 0 :(得分:0)
当您在jQuery Ajax选项中设置dataType: "json",
时,jQuery将为您解析响应。它为你运行JSON.parse()。
所以当你在做returnedData = JSON.parse(data);
时,你正在尝试解析而不是字符串。
没有必要解析数据,因为它已经通过jQuery从字符串转换为对象。
当您将数据类型更改为其他内容时,它的工作原因是因为另一个数据类型返回一个字符串,而不是一个对象,因此可以解析该字符串。
现在看看为什么会触发Ajax处理程序,这通常是由响应之前或之后输出的其他字符引起的。检查的方法是逃避输出。
console.log(escape(msg.responseText));
您会在{
之前或}
因此,您需要查看PHP代码,看看可以将这些字符写入响应中。