北城已荒凉 2015-02-03 15:38 采纳率: 0%
浏览 182

jQuery Ajax异常捕获器

I'm stuck with a simple exception that I can't catch:

//myScript.js.coffee
try
  $.ajax
    async: false
    type: "GET"
    url: index_url
    success: ->
      //Do something
    error: ->
      //Do something else
    fail: ->
      //Do something else
catch e
  console.log e

When I try to call this Ajax with a bad url, there is an error and nothing caught it. Does any one have an idea ?

Basically I would like to test the url before calling this ajax request.

Edit: I know that putting a try/catch with an asynchronous function is useless but it's to show that I tried different ways to catch those exceptions but nothing works.

  • 写回答

3条回答 默认 最新

  • weixin_33716941 2015-02-03 15:43
    关注

    Your sintax is wrong:

    try {
        $.ajax {
            async: false,
            type: "GET",
            url: index_url,
            success: function(data) {
                alert ("post is success");
            },
            error: function(request,error) { 
                alert(request.responseText);
                alert(error);
            }
        }
    } catch (e) {
        console.log(e);
    }
    

    But as suggested, you can use the ajax response to do what you want depending the result...

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型