ajax抛出错误与正确的结果 [英] ajax throwing error with right outcome

查看:255
本文介绍了ajax抛出错误与正确的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能:

  side ='car'; 

var request_checkUrl ='/ antibot / antibot_data?script ='+ side;

$ .ajax({
url:request_checkUrl,
dataType:'application / json',
complete:function(data){
alert running ..);
},
success:function(data){
alert(success);
},
error:function ){
alert(ERROR);
console.log(data);
}

})

结果是:
该框有ERROR,并且正在运行。它也会在控制台日志中显示:







回应正确。



我也尝试过get,getjson,ajax和数据类型json等。但是,为什么这段代码会抛出错误信息,但我得到了相同的结果。



我在这里做了什么错误?



这里是完整的回应:

  {
status: 200,
data:{
text:en TV,
images:[
{
hash:47a32df0c4b1f0b522e5faf35a46aacd95fe0ed4,
file:ABImage_plane_1
},
{
hash:e11f83f4411364546329c8a8bf88da0dffd27029,
file:ABImage_house_2
} ,
{
hash:93b4454ac09e7d7478fa2d25322e0e784370ea7a,
file:ABImage_car_5
},
{
hash:36fac21a830b922edb507487d833556aeb9688f7 ,
file:ABImage_clock_4
},
{
hash:cd1df47e052a5d0d50dab61b3e716339be0c6e68,
file:ABImage_TV_3 b},
{
hash:59e7f70b7874a500e576e25077adf254c52f5ee8,
file:ABImage_train_4
}
]
}
}

EDIT2:



函数,使用服务器端PHP

  echo $ this-> framework-> ajaxJSONResponse(200,$ data); 

  function ajaxJSONResponse($ status,$ data){
header('Content-type:application / json');
$ response = array();
$ response ['status'] = $ status;
$ response ['data'] = $ data;
return json_encode($ response);
}

编辑:
im使用jquery 2.1.3

 < script src =// cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js >< / script> 

编辑:
我也试过:

  $。ajax({
url:request_checkUrl,
type:GET,
dataType:json,//xml ,json
success:function(data){

alert(success);
alert(data);

}
error:function(jqXHR,textStatus,ex){
alert(textStatus +,+ ex +,+ jqXHR.responseText);
}
}

输出错误:



a href =https://i.stack.imgur.com/frlXc.png =nofollow noreferrer>



更新时间:



我尝试验证网址:
http://www.freeformatter.com/json-validator.html



并获得:

  JSON输入无效根据RFC 4627(JSON规范)。意外的令牌{status:200,data:{text:en TV,images:[{hash:47a32df0c4b1f0b522e5faf35a46aacd95fe0ed4,file:ABImage_plane_1},... 

但如果我启用:


接受未引用的名称


我获得:

  JSON输入根据RFC 4627(JSON规范)有效。 

如何使我的请求也能工作?



尝试将您的代码修改为类似于下面的代码: this:

  var side ='car'; 
var request_checkUrl ='/ antibot / antibot_data?script ='+ side;

$ .ajax({
url:request_checkUrl,
type:GET,
dataType:'json',
beforeSend:function {
document.title =Running ...;
},
success:function(data){
document.title =Success。;
alert(success);
},
错误:function(data){
alert(ERROR);
console.log(data);
}
});

让我知道这是否有效。



更新:



在上次更新时,您将收到无效的json。因为PHP你需要打印一个有效的json,总是。您可以使用此链接验证您的json输出: http://pro.jsonlint.com



parsererror,SintaxError:意外的令牌:这意味着你在PHP代码中有错误。


I have the following function:

side= 'car';

    var request_checkUrl = '/antibot/antibot_data?script=' + side;

 $.ajax({
    url: request_checkUrl,
    dataType: 'application/json',
    complete: function(data){
       alert("running..");
    },
    success: function(data){
        alert("success");
    },
    error: function(data) {
        alert("ERROR");
        console.log(data);
    }

})

the results are: the box with ERROR, and the running. it also shows this in the console log:

tthe response is correct. but, why does this piece of code throw me error message, when it should be success?

I also tried with get, getjson, ajax with datatype json, etc.. but i get the same outcome.

What have i done wrong here?

EDIT:

here is the full response:

{
    "status": 200,
    "data": {
        "text": "en TV",
        "images": [
            {
                "hash": "47a32df0c4b1f0b522e5faf35a46aacd95fe0ed4",
                "file": "ABImage_plane_1"
            },
            {
                "hash": "e11f83f4411364546329c8a8bf88da0dffd27029",
                "file": "ABImage_house_2"
            },
            {
                "hash": "93b4454ac09e7d7478fa2d25322e0e784370ea7a",
                "file": "ABImage_car_5"
            },
            {
                "hash": "36fac21a830b922edb507487d833556aeb9688f7",
                "file": "ABImage_clock_4"
            },
            {
                "hash": "cd1df47e052a5d0d50dab61b3e716339be0c6e68",
                "file": "ABImage_TV_3"
            },
            {
                "hash": "59e7f70b7874a500e576e25077adf254c52f5ee8",
                "file": "ABImage_train_4"
            }
        ]
    }
}

EDIT2:

this is the function that im using server sided PHP

echo $this->framework->ajaxJSONResponse(200, $data);

and

 function ajaxJSONResponse($status, $data)    {
        header('Content-type: application/json');
        $response             = array();
        $response['status']   = $status;
        $response['data']     = $data;
        return json_encode($response);
    }

EDIT: im using jquery 2.1.3

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

EDIT: I also tried:

$.ajax({
    url: request_checkUrl,
    type: "GET",
    dataType: "json", // "xml", "json"
    success: function (data) {

        alert("success");
        alert(data);

    },
    error: function (jqXHR, textStatus, ex) {
        alert(textStatus + "," + ex + "," + jqXHR.responseText);
    }
});

with the output error:

UPDATE AGAIN:

i tried to validate url with: http://www.freeformatter.com/json-validator.html

and got:

The JSON input is NOT valid according to RFC 4627 (JSON specification). Unexpected token {"status":200,"data":{"text":"en TV","images":[{"hash":"47a32df0c4b1f0b522e5faf35a46aacd95fe0ed4","file":"ABImage_plane_1"},...

but if i enable:

Accept non-quoted names

i get :

The JSON input is valid according to RFC 4627 (JSON specfication).

how can i make this work on my request too?

解决方案

The following answer just try to help with a possible solution.

Try to fix your code to something like this:

var side = 'car';
var request_checkUrl = '/antibot/antibot_data?script=' + side;

$.ajax({
    url: request_checkUrl,
    type: "GET",
    dataType: 'json',
    beforeSend: function () {
        document.title = "Running...";
    },
    success: function (data) {
        document.title = "Success.";
        alert("success");
    },
    error: function (data) {
        alert("ERROR");
        console.log(data);
    }
});

Let me know if this works.

Update:

In your last update, you are getting an invalid json. Since PHP you need to print a valid json, always. You might use this link to validate your json output: http://pro.jsonlint.com.

parsererror, SintaxError: Unexpected token: It means, you've something wrong in php code.

这篇关于ajax抛出错误与正确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆