weixin_33691598 2015-11-29 06:20 采纳率: 0%
浏览 24

AJAX多个Ajax调用

I have the below code and it is acting in a way that I dont understand. The purpose seems pretty straightforward to me, if the first ajax call is successful, then get the loggedInUser from another ajax GET call and reload the page to the link created. My Code looks like

function getLoggedInUser() {    
  var username = "";    
  $.ajax({
    url: '/api/getLoggedInUser',
    data: [],
    dataType: 'json',
    type: 'GET',
    success: function(response) {
      alert("a " + username);
      username = response.username;
      alert("b " +username);
      var link = "/_marshall/rssadd/";
      link = link+username;
      alert("c " + link);

    },
    error: function(xhr, status, error) {
    var err = eval("(" + xhr.responseText + ")");
    //alert("Please Try Again, we had an internal error!");
      alert(err.message);
      args['error'] = "1";
    }
  });

  alert("d " + username);
  return username;
}

$(document).ready(function ()
{    
  $("#rssCreateSubmit").click(function(event) {    
    var rssInfo = {}    
    rssInfo["title"] = $("#rssCreateTitle").val();
    rssInfo["category"] = $("#rssCreateCategory").val();
    rssInfo["copyright"] = $("#rssCreateCopyright").val();
    rssInfo["description"] = $("#rssCreateDescription").val();

    $.ajax({
      url: '/api/createRSS',
      data: rssInfo,
      dataType: 'json',
      type: 'POST',
      success: function(response)
      {
        var loggedInUser = getLoggedInUser();
        alert("e " + loggedInUser);
      },
      error: function(xhr, status, error)
      {
      var err = eval("(" + xhr.responseText + ")");
      //alert("Please Try Again, we had an internal error!");
      alert(err.message);
      }
    });
  });

});

The alerts print out in this order, which I do not understand. I think it has something to do with ajax being asynchronous, but I am not too sure what that means and I still feel like this code should work. Thanks.

"d "
"e "
"a "
"b value"
"c /_marshall/rssadd/value"

And the html link in changes to the below instead of rerouting:

http://www.bugbounty.design/_marshall/rsscreate/testMarshall1?rssCreateCategory=&InputEmail=
  • 写回答

2条回答 默认 最新

  • 衫裤跑路 2015-11-29 06:30
    关注

    Ajax calls are taken out of the main execution thread. This is why 'd' and 'e' are printed first, since they are not in an ajax call.

    I found this video really helpful for understanding ajax https://www.youtube.com/watch?v=8aGhZQkoFbQ

    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了