Access the URL of an jQuery Ajax Request in the Callback Function

前端 未结 6 1850
暖寄归人
暖寄归人 2020-12-08 18:20

Is there a way that I can see the URL that was requested when I do an Ajax request with jQuery?

e.g.,

var some_data_object = { ...all sorts of junk..         


        
6条回答
  •  无人及你
    2020-12-08 19:10

    Here is a possible solution:

    1. Catch the ajax call before it is sent to the server by implementing the beforeSend callback function.
    2. Save the url and the data
    3. Report it in the error message you generate.

      var url = "";
      
      $.ajax({
        url: "/Product/AddInventoryCount",
        data: { productId: productId, trxDate: trxDate, description: description, reference: reference, qtyInCount: qtyInCount }, //encodeURIComponent(productName)
        type: 'POST',
        cache: false,
        beforeSend: function (jqXHR, settings) {
          url = settings.url + "?" + settings.data;
        },
        success: function (r) {
          //Whatever            
        },
        error: function (jqXHR, textStatus, errorThrown) {
          handleError(jqXHR, textStatus, errorThrown, url);
        }
      });
      
      function handleError(jqXHR, textStatus, errorThrown, url) {
         //Whatever
      }
      

提交回复
热议问题