0

Actually I'm trying to a send ajax call with a data array. This is my sample code, criteria is an array but when I send this call it doesnt return any data. but when I tried it using php it worked perfectly. This is my sample php code

$post = array('criteria'=>array('user_id'=>'user1','subject'=>'meeting'));
$fields = (is_array($post)) ? http_build_query($post) : $post;

$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://mydomain/task/find');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        
    $serverOutput = curl_exec ($ch);
    curl_close ($ch);
    error_log('>>>>>>>>>>>>>>>>'.$serverOutput);

So what is the mistake of following code? How can I send an array using ajax ? please help me.

$.ajax({
    type: "POST",
    url: "http://mydomain/task/find",
    data: { "criteria" : [{'user_id':'uesr1'},{'subject':'meeting'}]},
    success: function(data) {
        if (data.status) {
            alert('ok');
        }
    },
    dataType: 'json',
});

actually this works for single element like this,

 $.ajax({
    type: "POST",
    url: "http://mydomain/task/find",
    data: { "criteria" : [{'user_id':user1}]},
    success: function(data) {
        if (data.status) {

        }
    },
    dataType: 'json',
});

2 Answers 2

0

When a multi-dimensional array is passed to http_build_query it act in a way that is unique and doesn't really exists in javascript (It is more common to use json encoded objects to send complex data)

The equivalent data object to your PHP code would be something like :

data: { "criteria[user_id]" : "uesr1", "criteria[subject]" : "meeting" },

The php.js library got an equivalent of the php function written in JavaScript : http://phpjs.org/functions/http_build_query/

How is the encoding done

There is an example in the php documentation :

<?php
$data = array('user'=>array('name'=>'Bob Smith',
                            'age'=>47,
                            'sex'=>'M',
                            'dob'=>'5/12/1956'),
              'pastimes'=>array('golf', 'opera', 'poker', 'rap'),
              'children'=>array('bobby'=>array('age'=>12,
                                               'sex'=>'M'),
                                'sally'=>array('age'=>8,
                                               'sex'=>'F')),
              'CEO');

echo http_build_query($data, 'flags_');
?>

And the resulting POST data (url decoded and wrapped for better understanding) :

  user[name]=Bob Smith
& user[age]=47
& user[sex]=M
& user[dob]=5/12/1956
& pastimes[0]=golf
& pastimes[1]=opera
& pastimes[2]=poker
& pastimes[3]=rap
& children[bobby][age]=12
& children[bobby][sex]=M
& children[sally][age]=8
& children[sally][sex]=F
& flags_0=CEO

As you can see the array is flattened with brackets used to identify levels, passing the same object in jQuery as data would have generated something like :

  user={"name": "Bob Smith", "age": "47", "sex"="M", "dob": "5/12/1956"}
& passtimes=["golf", "opera", "poker", "rap"]
& children={"bobby": { "age": "12", "sex": "M"}, "sally": { "age": "8", "sex": "F"}
& flags_0="CEO"

If you don't know about the format used to encode POST data, see the wikipedia article on POST it's well writen.

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

2 Comments

wow it works !!!, but I can't understand why ? if u can please explain. thanks !!!
I added more infos with the same sample as the one in the php docs but formated for readability
0

try using this syntax

 data:({
                criteria :[{
                    user_id: "user1",
                    subject:"meeting"
                }]
            })

1 Comment

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.