尝试/抓住jquery ajax请求 [英] Try/Catch with jquery ajax request

查看:95
本文介绍了尝试/抓住jquery ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个提供ajax请求的Google Chrome扩展程序。类似于GMail Checker扩展。问题是当我使用jquery进行请求时,我输入错误的用户名/密码,它会默认地失败,错误回调函数被忽略。



如果我将ajax调用从background.html脚本(在开发人员窗口中看不到请求),到options.html脚本,我将获得一个对话框进行重新身份验证。如果我点击取消,那么jquery错误回调触发。



但是,在原始的模型扩展(再次,Gmail检查器)中,他们使用纯(非jquery)ajax调用一个try / catch,如果我输入错误的凭据,我会得到一个警报说尽可能多。



我尝试将整个jquery调用包装在一个try /如下所示:

  try {
$ .ajax({
type:POST
url:someurl,
contentType:text / xml,
data:somedata,
username:user,
password:pass,
success:函数(data,status,xhr){
alert(Hurray!);
},
错误:function(xhr,status,error){
alert !+ xhr.status);
},
dataType:xml
});
} catch(e){
alert(你搞砸了!);
}

但仍然没有。



是否因为异步而导致错误,或者Chrome不会将请求返回为错误,因为它要重新提示凭据?或者我不知道如何使用try / catch?



更新



这是一个非常纤薄的版本的模型代码如何执行请求:

  var req = new XMLHttpRequest(); 
req.onreadystatechange = function(){
try {
if(req.readyState == 4){
//做一些结果的东西
}
}
catch(ex){
alert('解析响应错误');
}
}
try {
req.send(data);
}
catch(ex){
alert('请求发生错误');
}


解决方案

您有一个功能,每次请求完成后运行,无论发生什么。这应该解决问题:

  //存储全局变量,所以我们都可以很好的发挥。 

//确保在AJAX调用之前重新设置为false,
//或者只能工作一次!
var weHaveSuccess = false;

$ .ajax({
type:POST,
url:someurl,
contentType:text / xml,
data:somedata ,
username:user,
password:pass,
success:function(data,status,xhr){
alert(Hurray!);
weHaveSuccess = true;
},
error:function(xhr,status,error){
alert(Error!+ xhr.status);
},
complete :function(){
if(!weHaveSuccess){
alert('您的用户名/密码似乎不正确!);
}
},
dataType :xml
});


I am trying to build a Google Chrome extension that makes an ajax request. Something similar to the GMail Checker extension. The problem is that when I do the request using jquery, and I put in the wrong username/password, it fails silently, with the error callback function ignored.

If I move the ajax call out of the background.html script (where I can't see the requests in the developer window), to the options.html script, I get a dialog box to re-authenticate. If I hit cancel, THEN the jquery error callback fires.

But in the original model extension (again, the Gmail checker), they use plain (non-jquery) ajax calls with a try/catch, and if I put in the wrong credentials, I get an alert saying as much.

I tried wrapping the entire jquery call in a try/catch, like so:

try {
    $.ajax({
        type: "POST",
        url: someurl,
        contentType : "text/xml",
        data: somedata,
        username: user,
        password: pass,
        success: function(data,status,xhr){
            alert("Hurrah!");
        },
        error: function(xhr, status, error){
            alert("Error!" + xhr.status);
        },
        dataType: "xml"
    });
} catch(e) {
    alert("You messed something up!");
}

But still nothing.

Is the error due to it being asynchronous, or is Chrome not returning the request as an error since it wants to re-prompt for credentials? Or do I just not know how to use try/catch?

Update

Here is a very slimmed down version of how the model code does the request:

var req = new XMLHttpRequest();
req.onreadystatechange = function() {
    try {
        if ( req.readyState == 4 ) {
            //Do some stuff with results
        }
    }
    catch (ex) {
        alert('Error parsing response.');
    }
}
try {
    req.send (data);
}
catch (ex) {
    alert ('Something went wrong with the request.');
}

解决方案

Try something more like this, where you have a function that runs on every request once completed, regardless of what happens. This should solve the problem:

// Store a global var so we can all play nicely.

// Make sure this gets reset to false right before your AJAX call,
// or it will only work once!
var weHaveSuccess = false;

$.ajax({
    type: "POST",
    url: someurl,
    contentType : "text/xml",
    data: somedata,
    username: user,
    password: pass,
    success: function(data,status,xhr){
        alert("Hurrah!");
        weHaveSuccess = true;
    },
    error: function(xhr, status, error){
        alert("Error!" + xhr.status);
    },
    complete: function(){
        if(!weHaveSuccess){
             alert('Your username/password seems to be incorrect!');
        }
    },
    dataType: "xml"
});

这篇关于尝试/抓住jquery ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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