0

I am working on a school project that involves extracting product data from multiple online retailers using various APIs/AJAX calls, and then sorting this data in PHP (I use one AJAX call for each retailer). A snippet of the code I am working on is shown below. I cannot figure out how to

  1. Push a temporary array containing product attributes ("Average", "Price", "Name", "Url", and "Image") for each product into a master array (array of arrays) and then

  2. Post this master array to PHP in such a way that I can index the values in it for sorting purposes.

function get_results() {
  $(document).ready(function() {
    var master_array = [];
    $.ajax({
      type: "GET",
      url: "http//:www.source1.com",
      dataType: "xml",
      success: function(xml) {
        $(xml).find('product').each(function() {
          var Average = $(this).find('Average').text();
          var Price = $(this).find('Price').text();
          var Name = $(this).find('Name').text();
          var Url = $(this).find('Url').text();
          var Image = $(this).find('Image').text();
          master_array.push([Average, Price, Name, Url, Image]);
        });
      }
    });
    $.ajax({
      type: "GET",
      url: "http//:www.source2.com",
      dataType: "xml",
      success: function(xml) {
        $(xml).find('product').each(function() {
          var Average = $(this).find('Average').text();
          var Price = $(this).find('Price').text();
          var Name = $(this).find('Name').text();
          var Url = $(this).find('Url').text();
          var Image = $(this).find('Image').text();
          master_array.push([Average, Price, Name, Url, Image]);
        });
      }
    });
  });
}

1 Answer 1

1

You should see the example code for JQuery ajax function: http://api.jquery.com/jquery.ajax/. Here is an example code:

$.ajax({
  method: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
    alert( "Data Saved: " + msg );
});

Your updated function can look like this:

function get_results() {
  $(document).ready(function() {
    var master_array = [];
    $(xml).find('product').each(function() {
        var Average = $(this).find('Average').text();
        var Price = $(this).find('Price').text();
        var Name = $(this).find('Name').text();
        var Url = $(this).find('Url').text();
        var Image = $(this).find('Image').text();
        master_array.push([Average, Price, Name, Url, Image]);
    });
    $.ajax({
      type: "POST",
      url: "http//:www.source1.com",
      data: master_array,
      success: function(response) {
        alert('Data successfully posted');
      },
     fail: function(response) {
        alert('Data could not be posted');
      }
    });

  });
}

In the above code the success and fail are functions that are called when the servers returns a response. If the response was correctly sent then the success function is called. If there was an error on the server then the fail function is called.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.