简体   繁体   English

无法正确读取API调用(JS)的响应

[英]Can't correctly read response of API call (JS)

I'm calling an API and getting results back as expected in JavaScript. 我正在调用API并按预期在JavaScript中返回结果。 If a 400 status is returned, I call another API, which I'm doing with an if statement. 如果返回400状态,那么我将调用另一个API,该API正在使用if语句。 However, this "if" is never triggered, I think because the way my response is getting read. 但是,我认为从来没有触发过这个“如果”,这是因为我的回答被阅读了。 My code is: 我的代码是:

 var getSearchPrimary = {
                    url: 'SEARCH_URL',
                    type: 'POST',
                    crossDomain: true,
                    dataType: 'jsonp',
                    context : this,
                    cache : true,
                    timeout: 60000,
                    success : function(e) {
                        status = JSON.stringify(e.status.code);
                        if(status === "400"){
                            console.log("failed");
                            this.getSearchSecondary(query);
                        }else{
                        console.log(JSON.stringify(e));
                        }

In this case, when I do console.log(status); 在这种情况下,当我执行console.log(status); I get "400" so I'm really not sure why my if statement is never firing! 我得到"400"所以我真的不确定为什么我的if语句永远不会触发!

The response is: 响应为:

{"status":{"code":"400","message":"Error: No results match the query"},"response":{"results":""}} {“状态”:{“代码”:“ 400”,“消息”:“错误:没有结果与查询匹配”},“响应”:{“结果”:“”}}

I've been staring at this a while now, and would really appreciate any insight!! 我一直在凝视这一段时间,非常感谢任何见识!!

The "success" method is called when the request is successful. 请求成功时,将调用“成功”方法。 When 400 is returned, that's not a success. 当返回400时,这并不成功。 Use the "error" property instead to provide a function that is executed when the request fails. 请改用“错误”属性来提供在请求失败时执行的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM