jQuery Ajax 去往 [object%20Object]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7229205/
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
Ajax going to [object%20Object]
提问by user781655
I'm just trying to do a simple request like this:
我只是想做一个这样的简单请求:
$('.asd').change(
function () {
$.ajax({
url: 'http://127.0.0.1/folder/index.php/controller/action/integer',
success: function(data){}
});
}
);
This code tries to go to http://127.0.0.1/folder/index.php/controller/[object%20Object] instead and gets a 404. Where is it pulling the object from? I'm using a simple string.
此代码尝试转到 http://127.0.0.1/folder/index.php/controller/[object%20Object] 并获得 404。它从哪里拉出对象?我正在使用一个简单的字符串。
回答by sffc
For me the issue was that I was using $.post
instead of $.ajax
.
对我来说,问题是我使用的是$.post
而不是$.ajax
.
// fails:
$.post({
url: "/example/" + this.id,
// ...
});
// works:
$.ajax({
url: "/example/" + this.id,
// ...
});
回答by Matthias M
ajax
expects a parameter map.
post
expects single parameters:
ajax
需要一个参数映射。
post
期望单个参数:
// fails:
$.post({
url: "/example/" + this.id,
// ...
});
// works:
$.post("/example/" + this.id);
回答by DaveG
I had the same issue and dug all over looking for an answer. Unfortunately, this contributor never came back with one. Mine was a stupid error. upon returning from Ajax, I used inadvertently named my variable after a reserved word. Here is what I had:
我遇到了同样的问题,并四处寻找答案。不幸的是,这位贡献者再也没有回来。我的是一个愚蠢的错误。从 Ajax 返回后,我无意中使用了保留字后命名我的变量。这是我所拥有的:
$.post('/MyApp.php', { param: 'getLocation' },
function(xml) {
location=$(xml).find('Location');
}
});
Coded like this, upon return from Ajax the page is redirected to http://myurl/[Object%20object] which makes perfect sense now.
像这样编码,从 Ajax 返回后,页面被重定向到 http://myurl/[Object%20object],这现在非常有意义。
Resolution: change the "location=$..." to "clocation=$..." Hopefully this answer will help someone else. It's a tough one to debug.
解决方案:将“location=$...”更改为“clocation=$...”希望这个答案对其他人有所帮助。调试起来很困难。
回答by Techmag
For me, working in Play and using jsRoutes this was very very painful to figure out.
对我来说,在 Play 中工作并使用 jsRoutes 很难弄清楚。
It seems that Play (javaScript) will do an Ajax GET without issue using the following syntax:
似乎 Play (javaScript) 将使用以下语法毫无问题地执行 Ajax GET:
$.ajax(jsRoutes.controllers.MyController.ajax(inurlparam))
.done(function(data) {
}).fail(function(data) {
})
However (and here is the nasty part)... If you wish to do a POST you need to use the absolutURL()
method on the URL first to get the ajax (or post) not to include an [object%20Object]
into the url at post time. No amount of debugging the url or the form params seemed to indicate what was happening nor why. I just literally "guessed" this solution out of sheer frustration. PS: Note the brackets on the end of absolutURL()
-- js people will instantly get those mean "do" the function not just pass me the handle to it.
但是(这里是令人讨厌的部分)...如果您希望进行 POST,您需要首先使用absolutURL()
URL 上的方法来获取 ajax(或发布),不要[object%20Object]
在发布时将 an 包含到 url 中。没有多少调试 url 或表单参数似乎表明发生了什么或为什么。我只是完全出于沮丧而“猜测”了这个解决方案。PS:注意末尾的括号absolutURL()
-- js 人们会立即得到那些意思是“做”这个函数,而不仅仅是把它的句柄传递给我。
var url = jsRoutes.controllers.MyController.ajaxPost(inurlparam).absoluteURL();
var formData = $('#form').serialize();
$.ajax({
url: url,
type: "POST",
data: formData
}).done(function(data) {
}).fail(function(data) {
})
I'm answering this here as this is where Google lead me once I finally realized it might not be my coding that was causing the problem :)
我在这里回答这个问题,因为这是谷歌引导我的地方,一旦我终于意识到可能不是我的编码导致了问题:)
回答by MsDeveloper
You Must Desriallize the text to Number(Int):
您必须将文本反序列化为 Number(Int):
var ID = parseInt(
$(this)
.closest("tr") //tr of DataTable
.find("td") //td of ....
.eq(0)
.text(),
10
);