最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - 2 urls in one ajax call? - Stack Overflow

programmeradmin5浏览0评论

I was wondering if I can do this jquery ajax on my code:

$.ajax({
  type: "POST",
  dataType: 'json',
  url: "functions/ajaxNca_add.php", "functions/ajaxNca_update.php",
  data: dataString,
  cache: false,
  success: function(response){
    // show success
    alert(response.a);
}

The code above is just an example and I knew that it's not working. How can I call 2 php script in just one ajax request in jquery ? Can someone help ?

I was wondering if I can do this jquery ajax on my code:

$.ajax({
  type: "POST",
  dataType: 'json',
  url: "functions/ajaxNca_add.php", "functions/ajaxNca_update.php",
  data: dataString,
  cache: false,
  success: function(response){
    // show success
    alert(response.a);
}

The code above is just an example and I knew that it's not working. How can I call 2 php script in just one ajax request in jquery ? Can someone help ?

Share Improve this question asked Jan 10, 2015 at 4:25 LarcyLarcy 2993 gold badges9 silver badges18 bronze badges 3
  • Why just one request? – Musa Commented Jan 10, 2015 at 4:27
  • Your success function expects responce from one php-script. How did you manage to handle two responce from two php-scripts? – msangel Commented Jan 10, 2015 at 4:28
  • you can not use more than on url in ajax. but you can implement the functionality of two php files in one and return the result in success. – Suchit kumar Commented Jan 10, 2015 at 4:32
Add a ment  | 

4 Answers 4

Reset to default 5

No you can not do this. If you will look at jquery ajax documentation, you will see that url accepts only string, not an array of stings.

You should either make two requests, or create another *.php entry point which will bine both php scripts and make a call to this one.

You can't call 2 url simultaneously.but you can call one after another.

        $.ajax({
      type: "POST",
      dataType: 'json',
      url: "functions/ajaxNca_add.php",
      data: dataString,
      cache: false,
      success: function(response){
$.ajax({
      type: "POST",
      dataType: 'json',
      url: "functions/ajaxNca_update.php",
      data: dataString,
      cache: false,
      success: function(response){
//responce
}

    });
}

    });

or

$.ajax({
          type: "POST",
          dataType: 'json',
          url: "functions/ajaxNca_add.php", 
          data: dataString,
          cache: false,
 async: true,
          success: function(response){
//responce
}});
 $.ajax({
          type: "POST",
          dataType: 'json',
          url: "functions/ajaxNca_update.php",
          data: dataString,
          cache: false,
 async: true,
          success: function(response){
//responce
}});

async: true - synchronizes your ajax calls

More simple way :

You can also do one ajax call to functions/ajaxNca_add.php file, and include second file functions/ajaxNca_update.php in first ajax file.

So from one ajax call, you can access 2 files at a time.

You can define the object first then you need to call ajax two times by changing the url.

var ajaxObj = {
    type: "POST",
    data: thisForm.serialize(),
    success: function (response) {
        if(response.status=="success"){
            dataLayer.push({'event' : 'requestdemo_form_submitted', 'request_demo_submit_event_gtm' : 'request_demo_submit_event_gtm'});
            $('.sidebar-req-sec').addClass('hide');
            $('.sidebar-req-sec-thnk').removeClass('hide');
            $('.sidebar-req-sec-thnk').addClass('show');
        }
    }
};
//url1 ajax 
ajaxObj.url = 'url1';
jQuery.ajax(ajaxObj);
//url2 ajax
ajaxObj.url = 'url2';
jQuery.ajax(ajaxObj);
发布评论

评论列表(0)

  1. 暂无评论