简体   繁体   English

如何使用错误响应进行JSON API调用?

[英]How to do a JSON api call with error response?

I use Javascript to retrieve through a JSON api call the amount of active products pasted in a certain filter. 我使用Javascript通过JSON API调用来检索粘贴在特定过滤器中的有效产品的数量。 My typicall response would be 我的典型回应是

{"total":34,"product_ids":["PRODUCT1","PRODUCT2",....."]}

My script is working fine when products are present but when none of the products are active the response will be: 当存在产品时,我的脚本运行良好,但是当没有产品处于活动状态时,响应将是:

{"error":"No products found, please check request settings"}

In this case the script will crash. 在这种情况下,脚本将崩溃。

What I tried to do is to set the var NumEdPicks to 0 when I get an error but I don't really know how as the script is crashing when it doesn't find "total". 我试图做的是在收到错误消息时将var NumEdPicks设置为0 ,但我真的不知道当找不到“总计”时脚本崩溃了。

This is what the retrieve part of the script looks like 这就是脚本的检索部分的样子

// Retrieve
var url = 'http://api.jetlore.com/products/products_by_filter.json?jl_cid=' + clientID + '&filter=' + filterName + '&per_page=' + maxCount + '&page=1';
var response = HTTP.Get(url);
var responseObj = Platform.Function.ParseJSON(response["Content"]);
var NumEditorsPick = responseObj.total;
if(NumEditorsPick>maxCount){ var NumEditorsPick = maxCount;}

I would like to set NumEditorsPick to 0 when I get the error response. 当我收到错误响应时,我想将NumEditorsPick设置为0。 Some things I was thinking about but which isn't working: 我正在考虑的一些事情,但是没有用:

var NumEditorsPick = responseObj.total || 0

or 要么

var NumEditorsPick = ‘total’ in responseObj ? responseObj.total : 0

How to define NumEditorsPick when there is no total? 没有总数时如何定义NumEditorsPick

I've tried so far: 到目前为止,我已经尝试过:

if (responseObj.hasOwnProperty('total')){
  var NumEditorsPick = responseObj.total;
}else{
  var NumEditorsPick = 0;
}

And

  if (responseObj.has("total")){var NumEditorsPick = responseObj.total;
}
  if (responseObj.has("error")){var NumEditorsPick = 0;
}

Both are crashing the execution of my script, so I'm starting to think that when there is an error response it just stops the script and ignores the rest, would that be possible? 两者都使我的脚本的执行崩溃,因此我开始认为,当发生错误响应时,它只会停止脚本,而忽略其余脚本,这可能吗? In that case, how to ignore this error response? 在这种情况下,如何忽略此错误响应?

EDIT: After using the try/catch method as suggested in the comments, I managed to finally make it work: 编辑:按照注释中的建议使用try / catch方法后,我终于设法使其工作:

var NumEditorsPick;

  try {
    var response = HTTP.Get(url);
    var responseObj = Platform.Function.ParseJSON(response["Content"]);
    NumEditorsPick = responseObj.total;
  } catch (error) {
    NumEditorsPick = 0;
  }

You can use Javascript's hasOwnProperty() to check if the parse JSON has the key you're looking for. 您可以使用Javascript的hasOwnProperty()来检查解析的JSON是否具有所需的密钥。

In this case, I'd be something like: 在这种情况下,我会是这样的:

var responseObj = Platform.Function.ParseJSON(response["Content"]);
if (responseObj.hasOwnProperty('error')){
    // handle error msg
}else{
    // do something else
}

Here's a simple example using the JSON input you've provided. 这是一个使用您提供的JSON输入的简单示例


Update 更新资料

Ok, so my initial answer was based on what you said here: 好的,所以我的最初答案是基于您在这里所说的:

My script is working fine when products are present but when none of the products are active the response will be: 当存在产品时,我的脚本运行良好,但是当没有产品处于活动状态时,响应将是:

{"error":"No products found, please check request settings"}

But the service you're calling does not return a JSON string containing the error. 但是您正在调用的服务不会返回包含错误的JSON字符串。 Instead it returns a 404 and therefore, any attempt to parse or use the response content is not valid. 而是返回404,因此,任何尝试解析或使用响应内容的尝试都是无效的。

So, to start, you could try wrapping your HTTP.Get(url) in a try/catch method and on the catch clause set the NumEdPicks to zero. 因此,首先,您可以尝试在try / catch方法中包装HTTP.Get(url) ,然后在catch子句中将NumEdPicks设置为零。

Another option would be to check HTTP.Get() method documentation to see if the response object has a status (eg: response.Status ) or if you can pass a callback function for response and error, like this example in AJAX: 另一个选择是检查HTTP.Get()方法文档,以查看响应对象是否具有状态(例如: response.Status ),或者是否可以传递用于响应和错误的回调函数,例如AJAX中的示例:

$.ajax({
    url: 'yourUrl',
    type: 'GET',
    success: function(data){ 
        // Set NumEdPicks to total and do other stuff 
    },
    error: function(data) {
        // Set NumEdPicks to zero
    }
});

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

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