0

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() {

                }
            });
1
  • array with string index its not array :-) its simply object, anyway try use data: JSON.stringify(parameter) Commented Dec 25, 2013 at 8:27

3 Answers 3

2

You are adding values to your array using string, this adds properties to your array object, not actual array values.

You have two choices:

  1. Either use object {} instead of new Array, or
  2. Use coord.push(12); coord.push(42); etc.

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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(",");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.