0

So I have this jquery function that is pulling data from another domain and i want to display that data with ajax when the user enters a value into the search box. As of now the app continuously gives me the error message I wrote from the error state no matter if I search by .productName or .itemCode. Any ideas what I'm doing wrong?

function foodQuery() {

  var key = "123456789";

  $("#searchinput").keyup(function() {
    var value = $(this).val();
    foodQuery(value);
  });

  function foodQuery(key) {    // key is passed as a parameter

  var foodURL = "http://api.example.com/items?key=" + key;

  $.ajax({
    url: foodURL,
    type: 'GET',
    contentType: "text/plain",
    dataType: 'json',
    success: function(json) {
        $.each(json.products, function(index, product) { 
            // build product block
            var htmlString = '<div class="product large-3 columns">';
            //open imgwrap
            htmlString += '<div class="imgwrap">';
            //get img src
            htmlString += ' <img class="item_img" src="http://api.example.com/assets/images/' + product.itemCode + '@2x.jpg" />';
            // close imgwrap
            htmlString += '</div>';
            // open textwrap
            htmlString += '<div class="textwrap">';
            // get productName
            htmlString += '<h1 class="product_headline">' + product.productName + '</h1>' ;
            // get itemCode
            htmlString += '<h4 class="item_id" >#' + product.itemCode + '</h4>';
            // get description
            htmlString += '<p class="product_desc">' + product.description + '</p>';
            // open price
            htmlString += '<div class="price">';
            // get price & close div
            htmlString += '<span class="dollar"><span class="usd">$</span>' + product.price + '</span> <span class="product_desc">per weight</span></div>'
            // close divs
            htmlString += '</div>';

            //console.log(htmlString);
            $('.listing').append( $(htmlString) );

       }); //end each

    }, // end success
    error: function(e) {
       console.log(e.message);
       $('.listing').append( '<h1 class="errmsg" >Sorry, there was an unkown error.</h1>' );
    } // end error
    }); // end ajax request

    $(".listing").html("");
  }
}
user2882684
  • 539
  • 1
  • 5
  • 10

1 Answers1

0

Since its from a different domain, you can try using jsonp

KaNa
  • 31
  • 4