php 5b 5d,%5Bobject%20Object%5D (404 not found) when attempting to submit via AJAX
问题
I am trying to pass variables via ajax to a PHP script that will run a MySQL query. However, I keep getting the error 404 Not Found with: "http://myurl/database/%5Bobject%20Object%5D".
It looks like it's trying to send the data to http://myurl/database/%5Bobject%20Object%5D instead of the PHP script I've defined. Really at a loss on this one... I thought putting the absolute URL in would work.
Below is my code. Would be eternally grateful for any help....
function insertData()
{
var dataid= $('#dataid').attr('value');
var industry = $('#industry').attr('value');
var geolocation = $('#geolocation').attr('value');
$.post({
type: "POST",
url: "http://myURL/database/InsertData.php",
data: "dataid="+ dataid+"&industry="+ industry +"&geolocation="+ geolocation,
});
return false;
};
回答1:
As andi said, $.post expects an URL string as a first parameter; if you want to pass an option object, you must use $.ajax.
When you try to call $.post(), the option object is used as an URL; in that process, it is cast to a string (and an object cast to a string becomes [object ] in Javascript; in this case, [object Object]), it gets URL-encoded (%5Bobject%20Object%5D), and, since it does not start with a / or a protocol name, it is interpreted as a relative URL and gets prefixed with the current protocol, domain and path. Then, since there are no more parameters, an empty POST AJAX request is sent to that URL.
Another problem is that you use & to separate the parameters; that should be done only in HTML, here you should just use &. (Or a data object, as Evan said, that way you don't need do think about encoding issues.) And .val() and .attr('value') are not the same; the first is the current value of the field, the second is whatever value it had when the page was loaded.
The simplest way to this correctly is this:
function insertData() {
var data = $('#dataid,#industry,#geolocation').serialize();
$.post('http://myURL/database/InsertData.php', data);
return false;
}
This assumes that the three fields have the same name and id as $.serialize uses name for the parameter name.
回答2:
You should change the $.post to $.ajax or pass the URL and data as parameters.
$.post is just a shorthand to $.ajax, and it takes the URL as the first paramter.
So it should be:
$.ajax({
type: "POST",
url: "http://myURL/database/InsertData.php",
data: "dataid="+ dataid+"&industry="+ industry +"&geolocation="+ geolocation
});
or, with $.post
$.post("http://myURL/database/InsertData.php", "dataid="+ dataid+"&industry="+ industry +"&geolocation="+ geolocation);
Remember that the docs are almost always helpful
回答3:
try sending your data in an object instead, like:
data: {
dataid : dataid,
industry : industry,
geolocation : geolocation
}
回答4:
Evan is right, tryy sending your data an an object, jquery will take care of properly URL encoding the values. String concatenations can be problematic in js.
回答5:
It seems to be a jquery bug. jQuery.post is a shorthand of jQuery.ajax({method:"post"});, so I changed my code to this, and it worked for me. Hope they will correct this issue.
Note, the issue still exists in 1.10.2 and 2.0.2 versions.
来源:https://stackoverflow.com/questions/10357445/5bobject20object5d-404-not-found-when-attempting-to-submit-via-ajax
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
