jquery中ajax請求后台數據成功后既不執行success也不執行error,此外系統報錯:Uncaught SyntaxError: Unexpected identifier at Object.success,但后台能夠返回數據,原代碼如下:
var source=[];
$.ajax({
type: "post",
url: "connectdb/select.jsp",
data: {database: "scmdb", selectsql: sql},
async: false, method: 'post',
dataType: "json",
success: function(data) {
eval("source="+data+";");
//source=eval(data);
alert("正確");
},
error: function(err) {
alert("錯誤");
}
});
return source;
主要原因在於后台返回的數據並非json格式,而在代碼中指定了 dataType: "json", 解決方法是將 json改為text,修改后的代碼如下:
var source=[];
$.ajax({
type: "post",
url: "connectdb/select.jsp",
data: {database: "scmdb", selectsql: sql},
async: false, method: 'post',
dataType: "text",
success: function(data) {
eval("source="+data+";");
//source=eval(data);
alert("正確");
},
error: function(err) {
alert("錯誤");
}
});
return source;
