locked
Use one AJAX function that include different URL variable values for URL property. RRS feed

  • Question

  • User1909155429 posted

    I want to use a function that resides in a external javascript file, from two different pages that includes a AJAX call. The URL has a reference to locations that reside on the two pages. So how may i select current URL without throwing error ?

    $.ajax({ type: 'POST', //url: 'Message_Reply.aspx/PlaceOrder', url: 'Mail_Dispute.aspx/PlaceOrder', contentType: "application/json; charset=utf-8", data: '{products:[' + data.join() + ']}', dataType: 'json', success: function (results) { alert(results.d); }, error: function () { alert('error'); } }); return false;

    Tuesday, March 30, 2021 12:57 PM

Answers

  • User475983607 posted

    IMHO, this is a poor design because there are dependencies between the JS file and the ASPX pages.  One option is to use a variable/hidden field on the two ASPX pages that has contains ASPX page file name.  Use a standard jQuery selector to fetch the file name from the hidden input/variable. 

    Another option is to wrap the AJAX call in a function.  The ASPX pages pass the URL to the function as well as any other information the function needs to complete the task.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 30, 2021 1:02 PM

All replies

  • User475983607 posted

    IMHO, this is a poor design because there are dependencies between the JS file and the ASPX pages.  One option is to use a variable/hidden field on the two ASPX pages that has contains ASPX page file name.  Use a standard jQuery selector to fetch the file name from the hidden input/variable. 

    Another option is to wrap the AJAX call in a function.  The ASPX pages pass the URL to the function as well as any other information the function needs to complete the task.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 30, 2021 1:02 PM
  • User1535942433 posted

    Hi peterthegreat,

    As far as I think,you cann't write multiple url in one ajax function. There is a solution is using $.each. You could for loop of the url list and call the ajax function. However,I don't recommend you using this way because the value you get will be covered.  You'll probably want to use $('#someDiv').append(). Just like this:

    var urls = ['/url/one','/url/two', ....];
    
    $.each(urls, function(i,u){ 
         $.ajax(u, 
           { type: 'POST',
    contentType: 'application/json;charset=utf-8'; data: '{products:[' + data.join() + ']}',
    dataType: 'json', success: function (results) {....... }, error: function () { alert('error'); } }); return false; } ); });

    Best regards,

    Yijing Sun

    Wednesday, March 31, 2021 2:12 AM