Jquery 来自chrome扩展的ajax调用失败
我正在尝试对生成xml的googlesuggest页面进行ajax调用。我使用了一个小黑客,似乎是工作和记录 代码如下:Jquery 来自chrome扩展的ajax调用失败,jquery,ajax,google-chrome-extension,google-suggest,Jquery,Ajax,Google Chrome Extension,Google Suggest,我正在尝试对生成xml的googlesuggest页面进行ajax调用。我使用了一个小黑客,似乎是工作和记录 代码如下: $.ajax({ url: 'https://suggestqueries.google.com/complete/search', data: { client: 'firefox', q: word, }, dataType: 'jsonp' }) .done(function(dataWeGotViaJsonp){ var len =
$.ajax({
url: 'https://suggestqueries.google.com/complete/search',
data: {
client: 'firefox',
q: word,
},
dataType: 'jsonp'
})
.done(function(dataWeGotViaJsonp){
var len = dataWeGotViaJsonp.length;
for(var i=0;i<len;i++){
alert(dataWeGotViaJsonp[i]);
}
});
其中“ob”是搜索的术语
更新:
这是我更新的代码,根据我在本文中提供的第一个链接中的说明,将$.ajax替换为$.getJSON
function process(word){
$.getJSON("https://suggestqueries.google.com/complete/search?callback=?",
{
"jsonp":"suggestCallBack", // jsonp callback function name
"q":word, // query term
"client":"firefox" // force youtube style response, i.e. jsonp
}
);
suggestCallBack= function(dataWeGotViaJsonp){
alert("asdas");
var len = dataWeGotViaJsonp.length;
for(var i=0;i<len;i++){
alert(dataWeGotViaJsonp[i]);
}
};
};
我不知道如何真正解析。我很好奇为什么它不能在标准的谷歌搜索下工作。这是一个非常好的测试工具。我设法让它工作,但它的行为与此链接指定的稍有不同,也有点奇怪。问题是我从$.ajax切换到了$.getJSON调用,我使用了文档中指出的内容,并且实际得到了google结果(选中),尽管我使用了
客户端:“youtube”
说明符。我想现在谷歌把它用于youtube和谷歌搜索,或者他们不知怎么搞混了
$.getJSON("https://suggestqueries.google.com/complete/search?callback=?",
{
"jsonp":"suggestCallBack", // jsonp callback function name
"q":query, // query term
"client":"youtube" // force youtube style response, i.e. jsonp
}
);
suggestCallBack = function (data) {
var suggestions = []; //creates void array
$.each(data[1], function(key, val) {
suggestions.push(val[0]); //moves every suggestion from the word into the array
})
suggestions.length = 1; //restricts the array to only the first element
suggestions = []; //resets the array to void for future calls
};
这对我来说非常有效,应该对任何人都有效。我认为它可能也适用于
$.ajax
,但只能使用客户端:'youtube'
您可以使用该方法。无法查看是否出现错误。Chrome开发者控制台显示错误?任何控制台上都没有错误。我要试试。现在就失败。我忘了提到我在csp上授予了如下权限:“内容安全策略”:“脚本src'self”https://suggestqueries.google.com/清单文件中的;对象src'self'
已授予url的权限:“”?授予的权限https://suggestqueries.google.com/complete/*"
准确。您试过https://suggestqueries.google.com
(不带尾随斜杠)在您的CSP中?
window.google.ac.h
(
["ob",[["obama",0,[]],
["obamacare",0,[]],
["obituaries",0,[]],
["obey",0,[]],
["oblivion",0,[]],
["obama phone",0,[]],
["oberlin college",0,[]],
["obama gun control",0,[]],
["obagi",0,[]],
["obsidian",0,[]]],
{"k":1,"q":"I3uqQqdI9GsurIoEbRJwRQ_P7Co"}]
)
$.getJSON("https://suggestqueries.google.com/complete/search?callback=?",
{
"jsonp":"suggestCallBack", // jsonp callback function name
"q":query, // query term
"client":"youtube" // force youtube style response, i.e. jsonp
}
);
suggestCallBack = function (data) {
var suggestions = []; //creates void array
$.each(data[1], function(key, val) {
suggestions.push(val[0]); //moves every suggestion from the word into the array
})
suggestions.length = 1; //restricts the array to only the first element
suggestions = []; //resets the array to void for future calls
};