When I try to post an array with string index via Ajax, no data will be transferred. Please let me know the reason.
var coord=new Array;
coord["x"]=12;
coord["y"]=12;
coord["w"]=44;
coord["h"]=66;
console.log(coord);
var parameter={coord:coord};
$.ajax({
type: 'POST',
data: parameter,
dataType: 'json',
context: this,
url:'http://localhost/server/main/crop_image',
success: function(response) {
},
error: function() {
},
complete: function() {
}
});
You are adding values to your array using string, this adds properties to your array object, not actual array values.
You have two choices:
{}
instead of new Array
, or coord.push(12); coord.push(42);
coord.push(12); coord.push(42);
etc. Hope that helps.
Change the line var coord = new Array;
to var coord = {}
(object not Array)
Arrays in javascript are objects so you can do coord["x"] = "bla bla"
(same as coord.x = "bla bla"
) but in JSON arrays are lists of objects, so jQuery ignores those non-list properties of the array objects when translating the data to JSON.
By changing coord to be an object and not Array jQuery will translate it to JSON object which works as you expect.
try something like this
var parameter={coord:coord.join()};
on client you will get csv value which you can again convert it into array depending on server side language
PHP
$pizza = "piece1,piece2,piece3";
$pieces = explode(",", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
JAVA
String[] ary = "piece1,piece2,piece3".split(",");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.