【问题标题】:jquery search function fires error message everytimejquery搜索功能每次都会触发错误消息
【发布时间】:2015-11-16 02:09:21
【问题描述】:

所以我有这个 jquery 函数,它从另一个域中提取数据,当用户在搜索框中输入一个值时,我想用 ajax 显示该数据。到目前为止,无论我是按 .productName 还是 .itemCode 搜索,应用程序都会不断地给我从错误状态写入的错误消息。任何想法我做错了什么?

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

【问题讨论】:

  • 错误信息是什么?
  • 我写的消息“抱歉,出现未知错误。”
  • 试试这个以获得更多关于你的错误的信息stackoverflow.com/a/1637051/3185456
  • 我的意思是真正的错误不是你写下的处理它。删除该错误块中的所有内容。
  • 好的,所以我得到了 Uncaught SyntaxError: Unexpected identifier GET api.example.com/items?key= 401 (Unauthorized)

标签: javascript jquery arrays json search


【解决方案1】:

由于来自不同的域,您可以尝试使用jsonp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 2021-03-23
    相关资源
    最近更新 更多