0

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");
        }
    };
8
  • 2
    Minor remark: you don't send data to PHP, you make an HTTP request to a server [which may have a PHP app listening]. Commented Feb 4, 2014 at 15:21
  • 3
    Add this on you .ajax call: type: 'POST', Commented Feb 4, 2014 at 15:22
  • Maybe I'm just missing it, but I don't see a URL anywhere in that call to $.ajax Commented Feb 4, 2014 at 15:26
  • @moonwave99: thanks, I'll upgrade my lingo ;) Commented Feb 4, 2014 at 15:30
  • What do you get if you console.log(addList) before the ajax request? Commented Feb 4, 2014 at 15:30

2 Answers 2

1

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.

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

12 Comments

Hi, thanks for helping, with the suggested changes, I get from PHP: "<b>Warning</b>: json_decode() expects parameter 1 to be string, array given in ..."
Have you tried my recommendation in the edit to my question, to see if that helps?
Yes, I've just tested it. It is returning: <b>Fatal error</b>: Cannot use string offset as an array in
im on my phone so can't really write out an example here but I will when I get into my office, in the meantime can you do a pastebin of your current js and php and put the link here?
You're welcome :) for future reference, the crux of the issue was that jQuery encodes json objects as post parameters automagically, so you do not need to amend the json on the way to the server, or decode as json on the server, those things are done for you - glad it's solved though
|
0

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...

Comments

Your Answer

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