Post to two tables simultaneously in AJAX

Asked

Viewed 191 times

1

the code below it is working, however my doubt is the following: There is some way to make a post in two different addresses simultaneously using this same code without repeating it?

For example, in the code below I insert the data in a table, but there are other fields of my page that go to another table. In this situation there is some trick that can be done in this same code or I would have to repeat it and insert the data from the other table?

$.ajax({  

        url:'http://localhost:8080/teste/rest/cadastro',  
        type:'POST',
        contentType: "application/json",
        data :  dados,      
        dataType: 'JSON',
        success: function(data) { 

            }  
    }); 

Thanks in advance!

  • One question, the other fields, which go to another table, some of this data depends on the other post?

  • Yes, it depends on that first post.

1 answer

2


It is not possible to use more than one url in the same request. Alternatively, you can use a function to send the request from a given url, thus not repeating the code.

Example:

    function sendMyAjax(URL_address){
       $.ajax({  
            url: URL_address,  
            type:'POST',
            contentType: "application/json",
            data :  dados,      
            dataType: 'JSON',
            success: function(data) { 

                }  
        }); 
    };

sendMyAjax('http://localhost:8080/teste/rest/cadastro');
sendMyAjax('http://localhost:8080/teste2/rest/cadastro2');

Source: Multiple url in same ajax call? is this possible?

  • Thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.