如何在jQuery中获取406错误(不可接受) [英] How to get 406 Error (not acceptable) in jquery

查看:366
本文介绍了如何在jQuery中获取406错误(不可接受)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在my.js中进行错误处理,在这里我要跨域调用到其他服务器,对于动态HTML模板,我也在使用Mustache.js.

I am doing error handling in my.js, where i am having cross domain call to other server, also for dynamic HTML template i am using Mustache.js.

$.getJSON(url, function(data, textStatus, xhr) {
  $.each(data, function(i, data) {
    introPage(data);
  });
}).fail(function(Error) {
        alert(Error.status);
}); 

function introPage(data )
{
    $.getJSON('myphp.php?jsoncallback=?&template='+testTemplate, function(msg) {
        // my operations

    }).error(function(data) {
            }); 
}

getJSON中具有.fail()来捕获 getJSON 中的错误.我能够捕获404错误(未找到),但是当涉及到406错误(不可接受或说输入无效)或401错误(未经授权的访问)时,.fail()似乎不起作用

We have .fail() in getJSON to catch the errors in getJSON. I am able to catch 404 error (not found) but when it comes to 406 error (not acceptable or say invalid input) or 401 error(unauthorised access), .fail() does not seems to work

但在控制台中引发错误

but throws the error in console

当我单击链接时.它以以下格式显示了jQuery回调中的错误

Also when i click the link. it shows error in jquery callback in below format

jQuery191014790988666936755_1378113963780({
  "error": {
    "code": 406,
    "message": "Not Acceptable: Sorry, There are no results to display for the given input."
  }
});

现在我想知道如何在我的JS中得到这个错误.我没有收到错误代码406和401.我需要该错误代码,以便显示适当的错误页面.

Now i am wondering how to get this error in my JS . i am not getting Error code 406 and 401.I need that error code so that appropriate error page be displayed.

谢谢

推荐答案

jsoncallback=?参数和响应jQuery191...780({...})来看,似乎jQuery正在生成JSONP请求. JSONP请求不受相同的来源策略限制,但确实有以下限制,如此处所述 :

Judging by the jsoncallback=? parameter and the response jQuery191...780({...}), it seems like jQuery is generating a JSONP request. JSONP requests are not subject to the same origin policy restrictions but they do have the following limitation as described here:

注意:跨域脚本不会调用此[错误]处理程序,并且 跨域JSONP请求.

Note: This [error] handler is not called for cross-domain script and cross-domain JSONP requests.

解决方案:

  1. 如果服务器允许跨域请求,则创建一个纯JSON请求
  2. 查看以下问题的答案:
  1. Create a pure JSON request if the server allows cross-domain requests
  2. Look at answers for these questions:
    • How do I catch jQuery $.getJSON [...] error when using JSONP?
    • jQuery ajax (jsonp) ignores a timeout and doesn't fire the error event

这篇关于如何在jQuery中获取406错误(不可接受)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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