Google新闻API提供了一个错误Uncaught SyntaxError:Unexpected token')' [英] Google News API gives an error Uncaught SyntaxError: Unexpected token ')'
问题描述
var val =MSFT;
$ .get('https://www.google.com/finance/company_news?q='+val+'&output=rss',
function(data){
console .log(data);
});
而且该代码在本地工作正常。但是当我试图从GitHub调用这个相同的函数时,它给我一个关于CORS的错误。
所以,我想用下面的客户端代码:
var nws_lbl =MSFT;
var news_url ='https://www.google.com/finance/company_news?q='+nws_lbl+'&output=rss&callback=? ;
$ .ajax({
url:news_url,
data:{
格式:'json'
},
错误:函数(){
console.log(获取数据时出错);
},
dataType:'jsonp',
成功:函数(数据){
console.log(data);
}
});
但是,我得到以下错误:
它总是会出错。不知道我错过了什么。任何人都可以提供帮助吗?
您可以通过 CORS proxy ,如下所示:
var val =MSFT,
proxyUrl ='https://cors-anywhere.herokuapp.com/',
targetUrl ='https://www.google.com/finance/company_news? q = '+ VAL +' &安培;输出= RSS';
$ b $ .get(proxyUrl + targetUrl,
function(data){
console.log(data);
});
代理请求 targetUrl
,获取响应,添加 Access-Control-Allow-Origin
响应头和其他所需的CORS头,然后将其传递回您的请求代码。并且添加的 Access-Control-Allow-Origin
标题的响应是浏览器所看到的,所以浏览器可以让您的前端代码实际访问响应。
就问题中的第二个代码片段而言,我认为它不适用于您的原因是 https://www.google.com/finance/company_news
没有任何方法可以返回JSONP。因此,格式
和 callback
params似乎不会导致您想要的内容,但会导致一些非JSONP响应,你的代码无法解析,因为它不是预期的格式。
Originally I wrote some code like this:
var val="MSFT";
$.get('https://www.google.com/finance/company_news?q='+val+'&output=rss',
function (data) {
console.log(data);
});
And that code is working fine locally. But when I am trying to call this same function from GitHub it is giving me an error about CORS.
So, I am trying with the following client-side code:
var nws_lbl = "MSFT";
var news_url = 'https://www.google.com/finance/company_news?q='+nws_lbl+'&output=rss&callback=? ';
$.ajax({
url: news_url,
data: {
format: 'json'
},
error: function () {
console.log("Error while getting data");
},
dataType: 'jsonp',
success: function (data) {
console.log(data);
}
});
But with that I am getting the following error:
In the network I am getting 200 that means response is correct.
It is always going to the error. Not sure what I am missing. Can anyone help?
You can get around the CORS restriction by making your request through a CORS proxy, like this:
var val="MSFT",
proxyUrl = 'https://cors-anywhere.herokuapp.com/',
targetUrl = 'https://www.google.com/finance/company_news?q='+val+'&output=rss';
$.get(proxyUrl + targetUrl,
function (data) {
console.log(data);
});
The proxy makes the request to targetUrl
, gets the response, adds the Access-Control-Allow-Origin
response header and any other CORS headers needed, then passes that back to your requesting code. And that response with the Access-Control-Allow-Origin
header added is what the browser sees, so the browser lets your frontend code actually access the response.
As far as the second code snippet in the question, I think the reason it doesn’t work for you is that https://www.google.com/finance/company_news
doesn’t have any way to return JSONP. So the format
and callback
params don’t seem to cause what you want but are causing some non-JSONP response instead, which your code fails to parse because it’s not in the expected format.
这篇关于Google新闻API提供了一个错误Uncaught SyntaxError:Unexpected token')'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!