如何在PHP中的ajax调用中返回错误状态?
I have this script code and it works perfectly,
CODE
<script>
function ajob(){
var a3=a.value
var a4=b.value
var a5=c.value
if(a3!='' && a4!=''){
$.ajax({
type:"get",
url:"addj.php?content=" + a3 + "," + a4 + "," + a5
});
lod();
}else{alert('Fill all fields')}
}
</script>
<form>
<table>
<tr><td>Job Name:</td>
<td><input id="a" name="jname" type="text"></td>
</tr>
<tr><td>Job Description:</td>
<td><input id="b" name="jd" style="margin: 2px 0 2px 0;" type="text"></td>
</tr>
<tr><td>Status:</td>
<td>
<select id="c" name="jstat" style="width:100%; height: 26px" >
<option>Active</option>
<option>Not Active</option>
</select>
</td>
</tr>
</table>
</form>
what i want to know is how can can i know if there is an error in my addj.php
file? Here is my addj.php
addj.php
require 'con.php';
$pieces = explode(",", $_GET['content']);
$jname=$pieces[0];
$jd=$pieces[1];
$jstat=$pieces[2];
$query=mysqli_query($con,"INSERT INTO job(job_name,job_desc,status) values('$jname','$jd','$jstat') ");
How do I return a failed status from my PHP code and how can I handle that error in my java script code?
我有这个脚本代码,它完美无缺, p>
代码 strong> p>
我想知道的 我怎么知道我的 addj.php strong> p>
如何从我的PHP代码返回失败状态以及如何在我的java脚本代码中处理该错误? p>
div>
&lt; script&gt;
function ajob(){
var a3 = a.value
var a4 = b.value
var a5 = c.value
if(a3!=''&amp;&amp; a4!=''){
$ .ajax({
type:“get”,
url:“addj.php?content = = “+ a3 +”,“+ a4 +”,“+ a5
});
lod();
} else {alert('填写所有字段')}
}
&lt; / script&gt; \ n
&lt; form&gt;
&lt; table&gt;
&lt; tr&gt;&lt; td&gt;作业名称:&lt; / td&gt;
&lt; td&gt;&lt; input id =“a”name =“jname”type =“text”&gt;&lt; / td&gt;
&lt; / tr&gt;
&lt; tr&gt;&lt; td&gt;职位描述:&lt; / td&gt;
&lt; td&gt;&lt; input id =“b” name =“jd”style =“margin:2px 0 2px 0;” type =“text”&gt;&lt; / td&gt;
&lt; / tr&gt;
&lt; tr&gt;&lt; td&gt;状态:&lt; / td&gt;
&lt; td&gt;
&lt; select id =“ c“name =”jstat“style =”width:100%; height:26px“&gt;
&lt; option&gt; Active&lt; / option&gt;
&lt; option&gt; Not Active&lt; / option&gt;
&lt; / select&gt ;
&lt; / td&gt;
&lt; / tr&gt;
&lt; / table&gt;
&lt; / form&gt;
code> pre>
addj.php code>文件中是否有错误? 这是我的
addj.php code> p>
require' con.php';
$ pieces = explode(“,”,$ _GET ['content']);
$ jname = $ pieces [0];
$ jd = $ pieces [1];
$ jstat = $ pieces [2];
$ query = mysqli_query($ con,“INSERT INTO job(job_name,job_desc,status)values('$ jname','$ jd','$ jstat')”); \ n code> pre>
Use success or error callback function and output data to console.
function ajob(){
var a3=a.value
var a4=b.value
var a5=c.value
if(a3!='' && a4!=''){
$.ajax({
type:"get",
url:"addj.php?content=" + a3 + "," + a4 + "," + a5,
success: function(dataReturn, textStatus, jqXHR) {
console.log(dataReturn);
},
error: function(jqXHR,textStatus,errorThrown){
console.log(textStatus,errorThrown);
}
});
lod();
}else{alert('Fill all fields')}
}
You can use the console of the browser. Try following link that has step by step.
Why the Ajax script is not running on IIS 7.5 Win 2008 R2 server?
Use this
$.ajax({
type:"get",
url:"addj.php?content=" + a3 + "," + a4 + "," + a5,
success: function(data) { alert("succsess") },
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status + " : " + xhr.responseText);
}
});
You can use this code to catch any ajax errors.
$.ajax({
type:"get",
url:"addj.php?content=" + a3 + "," + a4 + "," + a5
}).fail(function(error){
alert(error);
});
Hope it helps.