Home  >  Q&A  >  body text

javascript - jsonp调用百度搜索提示 显示两个错误,求解决

问题:当采用中文输入法打拼音过程中或输入空格时两个错误
1.Resource interpreted as Script but transferred with MIME type baiduapp/json: "http://suggestion.baidu.com/su?wd=&_=1407990774861". 这个是jQuery的警告
2.Uncaught TypeError: Cannot read property 'sug' of undefined



< script> //回调函数 function resultcallback(res){ var txt = res.s; var html = ''; for(var i in txt){ html+='<li>'+txt[i]+'</li>'; } $('#search-result').html(html); $('#suggest-search').show().css({position: 'absolute', top: $('#search-form').offset().top+$('#search-form').height()+10, left: $('#search-form').offset().left-1 }); } //绑定键盘事件 $('#search-input').bind('keyup',function(){ var searchText = $('#search-input').val(); var url = "http://suggestion.baidu.com/su?wd="+searchText+"&json=1&p=3&cb=resultcallback"; var $html = "<script src="+url+"><\/script>"; $('head').append($html); }); </script>

PHP中文网PHP中文网2748 days ago519

reply all(1)I'll reply

  • 迷茫

    迷茫2017-04-10 14:34:48

    searchText 转义下

    searchText=encodeURIComponent(searchText);
    

    更科学的做法是在这之前先trim一下 纯空格的情况 不要发请求

    reply
    0
  • Cancelreply