javascript - jsonp调用百度搜索提示 显示两个错误,求解决
PHP中文网
PHP中文网 2017-04-10 14:32:48
0
1
539

问题:当采用中文输入法打拼音过程中或输入空格时两个错误
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中文网

认证高级PHP讲师

Antworte allen(1)
迷茫

searchText 转义下

searchText=encodeURIComponent(searchText);

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

Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage