user882670
user882670

Reputation:

JQuery Ajax not sending data to PHP

myscript.js below is outputing:

[{"orcamento":"10","atual":"20","desvio":"","data":"2015-01-01","nome_conta":"BBB","nome_categoria":"abc","nome_entidade":"def"}]

myscript.js:

if (addList.length) {
            $.ajax($.extend({}, ajaxObj, {
                data: { "addList": JSON.stringify(addList) },
                success: function (rows) {
                    $grid.pqGrid("commit", { type: 'add', rows: rows });

                },
                complete: function () {
                    $grid.pqGrid("hideLoading");
                    $grid.pqGrid("rollback", { type: 'add' });
                    $('#consola').text(JSON.stringify(addList));
                }
            }));
        }

The JSON data above has to be sent to my script.php below:

if( isset($_POST["addList"]))            
{
    $addList = json_decode($_POST["addList"], true);
    var_dump ($addList);
    echo "test";
    exit();
}

Although the data is correct and myscript.php is being called it isn't returning anything. I get:

NULLtest

I tried using GET, instead of POST but the result is the same, what is wrong with the code above?

EDIT: Here's the ajaxObj used in the ajax request:

var ajaxObj = {
        dataType: "json",
        url:"../myscript.php",
        type: "POST",
        async: true,
        beforeSend: function (jqXHR, settings) {
            $grid.pqGrid("showLoading");
        }
    };

Upvotes: 0

Views: 218

Answers (2)

Keir Lavelle
Keir Lavelle

Reputation: 523

From the PHP Docs on json_decode:

NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

So it is most likely that there is some error in your JSON data that is preventing json_decode from parsing it correctly, I've ran that snippet through jsonlint and it does say that it's valid JSON, but it's worth checking a larger sample of the data you send to the server for inconsistencies.

Other than that, is there any reason that you are calling JSON.stringify on the data object prior to sending to the server? I would try just sending the object itself as the data parameter of your AJAX call like so:

        $.ajax($.extend({}, ajaxObj, {
             data: { "addList": addList },
            success: function (rows) {
                $grid.pqGrid("commit", { type: 'add', rows: rows });

            },
            complete: function () {
                $grid.pqGrid("hideLoading");
                $grid.pqGrid("rollback", { type: 'add' });
                $('#consola').text(JSON.stringify(addList));
            }
        }));

And see if that helps:

EDIT

I should have noticed in my original answer, you will not need to call json_decode on your posted data, jQuery encodes the data as post parameters correctly for you; It should be accessible within your PHP script as an associative array, try replacing your current var_dump statement in your PHP var_dump($_POST['addList'][0]['orcamento']); and you should be good to go.

Upvotes: 1

acbaltaci
acbaltaci

Reputation: 244

First of all, be sure you are posting to a php file, use firebug or similar tools to track your script..

I don't see the part you defined the target PHP file on your javascript file..

A regular javascript code can look like this :

jQuery.ajax({
     type : "post",
     dataType : "json",
     url : 'target.php',
     data : {foo:bar },
     success: function(response) {
        // do something with response...
     }
});  

If you see that you are posting to the right php file the right parameters on firebug, try to use $_REQUEST if $_POST not working..

Firebug will show you the response of PHP file.. so do a print_r($_REQUEST['addList']) to see what is going on...

Upvotes: 0

Related Questions