Ajax调用不断失败,没有错误 [英] Ajax Call keeps failing without error
问题描述
所以,我的ajax调用正常工作,我得到了绿灯旁边的Web检查,我看到被送到正确的参数背部和返回JSON对象。但是,它会自动转到错误。我加了一些错误处理,并跑出的想法。
有谁看到什么?
Javascript的
函数getMoreOlder(ID){
$阿贾克斯({
网址:getmore.php
键入:POST,
数据类型:JSON,
数据: {
postid:身份证,
键入:后来,
},
成功:函数(响应){
警报(成功);
对于(VAR I = 0; I< json.length;我++){
//警报(循环);
}
},
错误:函数(jqXHR,错误errorThrown){
如果(jqXHR.status&安培;&安培; jqXHR.status == 400){
警报(jqXHR.responseText);
}其他{
警报(出事了);
}
}
});
}
PHP脚本
的$ id = $ _ POST ['postid'];
$型= $ _ POST ['类型'];
如果($类型==后){
$ SQL =CALL`sp_BlogPosts_PageByPostID`('的$ id。');
$ EXEC = mysqli_query($康恩,$ SQL);
而($取= mysqli_fetch_assoc($ EXEC)){
$ OBJ =阵列(
PostID=>函数stripslashes(修剪(mysqli_real_escape_string($康恩,$取['PostID'])))
标题=>函数stripslashes(修剪(mysqli_real_escape_string($康恩,$取['标题'])))
内容=>函数stripslashes(修剪(mysqli_real_escape_string($康恩,$取['内容'])))
电子邮件=>函数stripslashes(修剪(mysqli_real_escape_string($康恩,$取[电子邮件])))
用户名=>函数stripslashes(修剪(mysqli_real_escape_string($康恩,$取['用户名'])))
Created_At=>函数stripslashes(SUBSTR(修剪(mysqli_real_escape_string($康恩,$取['created_at'属性])),0,10)));
回声json_en code($ OBJ);
}
使用示例
<按钮的onClick =getMoreOlder(1113);>加载更多帖子< /按钮>
示例返回
{PostID:1177,标题:偏离轨道,内容:这是一个邮报,电子邮件:我@雅虎.COM,用户名:ABC123,Created_At:14年12月2日}
要点</ P>
-
正确的变量都被发送POST
-
正确的信息在POST返回
-
始终返回错误出事了!
-
Web检查显示200 $ C $下POST请求
问题是你不断地呼应了json_en code,这将使无效的JSON输出。
例如:
{PostID:1177,标题:偏离轨道} {PostID:1177,标题:偏离轨道}
这是无效的JSON。所以,把你所有的数据到一个数组/对象,然后回声json_en code
$ objArr =阵列();
而($取= mysqli_fetch_assoc($ EXEC)){
$ objArr [] =阵列(
PostID=&GT;函数stripslashes(修剪(mysqli_real_escape_string($康恩,$取['PostID'])))
标题=&GT;函数stripslashes(修剪(mysqli_real_escape_string($康恩,$取['标题'])))
内容=&GT;函数stripslashes(修剪(mysqli_real_escape_string($康恩,$取['内容'])))
电子邮件=&GT;函数stripslashes(修剪(mysqli_real_escape_string($康恩,$取[电子邮件])))
用户名=&GT;函数stripslashes(修剪(mysqli_real_escape_string($康恩,$取['用户名'])))
Created_At=&GT;函数stripslashes(SUBSTR(修剪(mysqli_real_escape_string($康恩,$取['created_at'属性])),0,10))
);
}
回声json_en code($ objArr);
或作为意见建议获取所有数据立刻和回声指出,
回声json_en code($ exec-&GT; fetch_all(MYSQLI_ASSOC));
此外,您的for循环使用了错误的变量
的(VAR I = 0; I&LT; json.length;我++){
应
的(VAR I = 0; I&LT; response.length;我++){
So my ajax call works fine, i get a green light next to it in web inspector and I see the correct parameters being sent back and JSON objects being returned. However, it automatically goes to ERROR. I added some error handling and have ran out of ideas..
Does anyone see anything??
Javascript
function getMoreOlder(id) {
$.ajax({
url: "getmore.php",
type : "POST",
dataType : "json",
data: {
postid : id,
type: "later",
},
success: function(response){
alert("Success");
for (var i=0; i<json.length; i++) {
// alert("Loop");
}
},
error: function(jqXHR,error, errorThrown){
if(jqXHR.status&&jqXHR.status==400){
alert(jqXHR.responseText);
}else{
alert("Something went wrong");
}
}
});
}
PHP Script
$id = $_POST['postid'];
$type = $_POST['type'];
if($type == "later") {
$sql = "CALL `sp_BlogPosts_PageByPostID`('" . $id ."')";
$exec = mysqli_query($conn, $sql);
while($fetch = mysqli_fetch_assoc($exec)) {
$obj = array(
"PostID" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['PostID']))),
"Title" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['title']))),
"Content" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['content']))),
"Email" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['email']))),
"Username" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['username']))),
"Created_At" => stripslashes(substr(trim(mysqli_real_escape_string($conn, $fetch['created_at'])), 0 , 10)));
echo json_encode($obj);
}
Example Usage
<button onClick="getMoreOlder(1113);"> Load More Posts </button>
Example Return
{"PostID":"1177","Title":"Off track","Content":"Hi This is a Post", "Email":"me@yahoo.com", "Username":"Abc123", "Created_At":"12-2-14"}
Key Points
Correct variables are being sent in POST
Correct information is returned in POST
Always returns error Something went wrong!
Web Inspector shows 200 code for POST request
the problem is you are continually echoing out json_encode, this will make invalid json output.
For example:
{"PostID":"1177","Title":"Off track"}{"PostID":"1177","Title":"Off track"}
This is invalid json. So put all your data into an array/object and then echo json_encode
$objArr = array();
while($fetch = mysqli_fetch_assoc($exec)) {
$objArr[] = array(
"PostID" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['PostID']))),
"Title" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['title']))),
"Content" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['content']))),
"Email" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['email']))),
"Username" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['username']))),
"Created_At" => stripslashes(substr(trim(mysqli_real_escape_string($conn, $fetch['created_at'])), 0 , 10))
);
}
echo json_encode($objArr);
Or as suggested in the comments fetch all the data at once and echo out that
echo json_encode($exec->fetch_all(MYSQLI_ASSOC));
Also your for loop is using the wrong variable
for (var i=0; i<json.length; i++) {
should be
for (var i=0; i<response.length; i++) {
这篇关于Ajax调用不断失败,没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!