weixin_33743661 2010-11-09 21:52 采纳率: 0%
浏览 204

链式Jquery Ajax调用

I am trying to retrieve some information about any given webpage (namely the of the page and the of the domain) and then make a jquery asynchronous POST with the retrieved information. Unfortunately, the javascript execution reaches the $.post(), but never makes the actual web request. Here is my code:

$.get('../embed', {u: url}, function(html) {
    alert('got "' + html + '"');
    $.post('/media/add', { story: storyid, caption: caption, type: 5, title: title, content: html, meta: meta }, function(data) {
        var obj = jQuery.parseJSON(data); 
        var thumb = imageUrlFromMedia(obj);
        var clip = addToClipboard(obj.id, thumb || '/img/icons/embedly.png', obj.name);
    });
});

Is it even possible to make another ajax call in the success handler of $.get()? Has anyone here had any success with multiple chained HttpRequests before?

  • 写回答

1条回答

  • 喵-见缝插针 2010-11-09 22:13
    关注

    You can use $.ajax to find errors in the XML HTTP requests or JSON parsing (may not work for JSON parsing unfortunately). That should still display as text even if it fails to parse. Anyways, to get failure messages refactor as so

    $.ajax({
      url: '../embed',
      data: {u: url},
      success: function(html){
        ..
         $.post
        ..
      },
      error: function(XMLHTTPRequest, textStatus, errorThrown){
         //Danger Will Robinson
      }
    });
    

    If you don't see a second request, this is the request that is failing. If you still don't find an error, try doing the same with the $.post. You should consider refactoring $.post to $.ajax. By the way, parseJSON is automatic if you use $.getJSON instead of $.post. This is akin to dataType: 'json' within $.ajax.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!