javascript 在 jQuery 中附加了 [object%20Object] 的 JSON 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14419730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON Request appended with [object%20Object] in jQuery
提问by Daniel Groves
I'm trying to fetch a custom JSON feed I have written with jQuery using the getJSON
method. For an unknown reason the URL seems to be having cache_gen.php?location=PL4
stripped from the end and replaced with [object%20Object] resulting in a 404 error occurring.
我正在尝试使用该getJSON
方法获取我用 jQuery 编写的自定义 JSON 提要。由于未知原因,URL 似乎cache_gen.php?location=PL4
已从末尾剥离并替换为 [object%20Object],从而导致 404 错误发生。
Here's the jQuery I'm using:
这是我正在使用的 jQuery:
var fetchData = function() {
if (Modernizr.localstorage) {
var api_location = "http://weatherapp.dev/cache_gen.php";
var user_location = "PL4";
var date = new Date();
console.log(api_location + '?location=' + user_location);
jQuery.getJSON({
type: "GET",
url: api_location + '?location=' + user_location,
dataType: "json",
success: function(jsonData) {
console.log(jsonData);
}
});
} else {
alert('Your browser is not yet supported. Please upgrade to either Google Chrome or Safari.');
}
}
fetchData();
From the console log I can see the URL string is calculated correctly as: http://weatherapp.dev/cache_gen.php?location=PL4
从控制台日志中,我可以看到 URL 字符串被正确计算为: http://weatherapp.dev/cache_gen.php?location=PL4
However the second line in the console is: Failed to load resource: the server responded with a status of 404 (Not Found)
.
但是控制台中的第二行是:Failed to load resource: the server responded with a status of 404 (Not Found)
.
Can anyone point me in the right direction with this?
任何人都可以指出我正确的方向吗?
UPDATE 19/01/2013 23:15
更新 19/01/2013 23:15
Well, I've just converted so that is fits the docs perfectly using $.ajax
. I've also added a fail event and logged all of the data that gets passed to it.
好吧,我刚刚进行了转换,因此它非常适合使用$.ajax
. 我还添加了一个失败事件并记录了传递给它的所有数据。
var fetchData = function() {
if (Modernizr.localstorage) {
var api_location = "http://weatherapp.dev/cache_gen.php";
var user_location = "PL4";
var date = new Date();
var url = api_location + '?location=' + user_location;
console.log(url);
jQuery.ajax({
type: "GET",
url: api_location + '?location=' + user_location,
dataType: "json",
success: function(jsonData) {
console.log(jsonData);
},
error: function( jqXHR, textStatus, errorThrown ) {
console.log('textStatus: ' + textStatus );
console.log('errorThrown: ' + errorThrown );
console.log('jqXHR' + jqXHR);
}
});
} else {
alert('Your browser is not yet supported. Please upgrade to either Google Chrome or Safari.');
}
}
fetchData();
After this my console gives me the following information:
在此之后,我的控制台给了我以下信息:
http://weatherapp.dev/cache_gen.php?location=PL4
download_api.js:44textStatus: parsererror
download_api.js:45errorThrown: SyntaxError: JSON Parse error: Unable to parse JSON string
download_api.js:46jqXHR[object Object]
I have ensured the headers for the JSON feed are current, and the feed is definitely serving valid JSON (it effectively caches a 3rd party service feed to save costs on the API).
我已确保 JSON 提要的标头是最新的,并且提要肯定提供有效的 JSON(它有效地缓存了 3rd 方服务提要以节省 API 成本)。
采纳答案by Adam
The reason why you see this error:
您看到此错误的原因:
http://weatherapp.dev/cache_gen.php?location=PL4
download_api.js:44textStatus: parsererror
download_api.js:45errorThrown: SyntaxError: JSON Parse error: Unable to parse JSON string
download_api.js:46jqXHR[object Object]
Is because your JSON is invalid. Even if a response comes back from the server correctly, if your dataType is 'json' and the returned response is not properly formatted JSON, jQuery will execute the error function parameter.
是因为您的 JSON 无效。即使响应从服务器正确返回,如果您的 dataType 是 'json' 并且返回的响应格式不正确 JSON,jQuery 将执行错误函数参数。
http://jsonlint.comis a really quick and easy way to verify the validity of your JSON string.
http://jsonlint.com是一种非常快速简便的验证 JSON 字符串有效性的方法。
回答by Owen
I was running into the same issue today. In my case I was assigning a JSON object to a variable named 'location' which is a reserved word in JavaScript under Windowsand appearantly is a shorthand for windows.location! So the browser redirected to the current URL with [object%20Object] appended to it. Simple use a variable name other than 'location' if the same thing happens to you. Hope this helps someone.
我今天遇到了同样的问题。在我的例子中,我将一个 JSON 对象分配给一个名为“location”的变量,它是Windows 下 JavaScript 中的一个保留字,看起来是 windows.location 的简写!所以浏览器重定向到当前 URL 并附加了 [object%20Object]。如果同样的事情发生在你身上,简单地使用一个变量名而不是“位置”。希望这可以帮助某人。
回答by Adam
Check out the actual function usage:
查看实际的函数使用:
http://api.jquery.com/jQuery.getJSON/
http://api.jquery.com/jQuery.getJSON/
You can't pass on object parameter into $.getJSON
like with $.ajax
, your code should look like this:
您不能将对象参数传递给$.getJSON
like with $.ajax
,您的代码应如下所示:
jQuery.getJSON('api_location + '?location=' + user_location)
.done(function() {
//success here
})
.fail(function() {
//fail here
});
To maybe make it a little clearer, $.getJSON
is just a "wrapper function" that eventually calls $.ajax
with {type:'get',dataType:'JSON'}
. You can see this in the link I provided above.
为了也许使它更清晰一点,$.getJSON
仅仅是一个“包装功能”,最终调用$.ajax
带{type:'get',dataType:'JSON'}
。您可以在我上面提供的链接中看到这一点。