2015-08-22 47 views
0

所以我有這個jQuery的功能,是從另一個領域拉動數據,我想顯示該數據與Ajax時,用戶輸入一個值到搜索框中。截至目前,無論我通過.productName還是.itemCode進行搜索,應用程序都不斷向我提供從錯誤狀態寫入的錯誤消息。任何想法我做錯了什麼?jQuery的搜索功能每次觸發錯誤消息

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(""); 
    } 
} 
+1

什麼是錯誤訊息? – Identity1

+0

我寫的信息「對不起,有一個未知的錯誤。」 – user2882684

+0

試試這個來獲得更多關於你的錯誤信息 http://stackoverflow.com/a/1637051/3185456 –

回答

0

因爲從不同的域了,你可以嘗試使用jsonp