jQuery Ajax not sending data to PHP


user 882670

The following myscript.js output:

[{"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 must be sent into 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 called, it returns nothing. I get:

NULLtest

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

EDIT: This is 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");
        }
    };
Keir Lavelle

From the PHP documentation on json_decode :

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

So most likely there is some bug in your JSON data that is preventing json_decode from parsing it properly, I've run the snippet through jsonlint and it does say it's valid JSON, but it's worth checking a larger sample of data you're sending Inconsistency to the server.

Other than that, is there any reason to call JSON.stringify on the data object before sending to the server? I would try sending the object itself as the data parameter of the AJAX call like this:

        $.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 the original answer that you don't need to call json_decode on the posted data, jQuery will correctly encode the data as a post parameter for you; it should be accessible in your PHP script as an associative array, try it in your replace the current var_dump statement in PHP and you var_dump($_POST['addList'][0]['orcamento']);should be good to go.

Related


jQuery Ajax not sending data to PHP

user 882670 The following myscript.js output: [{"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, {

jQuery ajax post not sending data to server side (using PHP)

username HTML <a id="1">Add to notebook</a> <a id="2">Add to notebook</a> <a id="3">Add to notebook</a> <a id="4">Add to notebook</a> ... <a id="40">Add to notebook</a> javascript <script> $(document).ready(function(){ //callback handler for post submit

jQuery ajax post not sending data to server side (using PHP)

username HTML <a id="1">Add to notebook</a> <a id="2">Add to notebook</a> <a id="3">Add to notebook</a> <a id="4">Add to notebook</a> ... <a id="40">Add to notebook</a> javascript <script> $(document).ready(function(){ //callback handler for post submit

jQuery ajax post not sending data to server side (using PHP)

username HTML <a id="1">Add to notebook</a> <a id="2">Add to notebook</a> <a id="3">Add to notebook</a> <a id="4">Add to notebook</a> ... <a id="40">Add to notebook</a> javascript <script> $(document).ready(function(){ //callback handler for post submit

jQuery ajax post not sending data to server side (using PHP)

username HTML <a id="1">Add to notebook</a> <a id="2">Add to notebook</a> <a id="3">Add to notebook</a> <a id="4">Add to notebook</a> ... <a id="40">Add to notebook</a> javascript <script> $(document).ready(function(){ //callback handler for post submit

AJAX not sending form data to PHP

Jerry I am sending some form data to PHP via AJAX. Before sending the data using AJAX, the value of the input field is displayed, but in the PHP script, the value received is NULL. $(document).ready(function(){ $('#my_form').submit(function(){

AJAX not sending form data to PHP

Jerry I am sending some form data to PHP via AJAX. Before sending the data using AJAX, the value of the input field is displayed, but in the PHP script, the value received is NULL. $(document).ready(function(){ $('#my_form').submit(function(){

AJAX not sending form data to PHP

Jerry I am sending some form data to PHP via AJAX. Before sending the data using AJAX, the value of the input field is displayed, but in the PHP script, the value received is NULL. $(document).ready(function(){ $('#my_form').submit(function(){

php not sending data using jquery

Guo Jiarik Here's a code with a DropDownlist that updates the database when it changes, for some reason it doesn't work. I realize the value $ gp_nameis not being emitted, can someone help me with it? <select name='status' id='status'> <option value="<?php ech

php not sending data using jquery

Guo Jiarik Here's a code with a DropDownlist that updates the database when it changes, for some reason it doesn't work. I realize the value $ gp_nameis not being emitted, can someone help me with it? <select name='status' id='status'> <option value="<?php ech

php not sending data using jquery

Guo Jiarik Here's a code with a DropDownlist that updates the database when it changes, for some reason it doesn't work. I realize the value $ gp_nameis not being emitted, can someone help me with it? <select name='status' id='status'> <option value="<?php ech

Ajax is sending data but Php file is not receiving data

awtthem I've spent the past 10 days or so trying to figure this out and I'm still stuck. I'm making an online budgeting system that sends Javascript values via Ajax to PHP which then sends them to a database. this is my javascript code $.ajax({ url: 'yup.ph

why jquery ajax is not sending data in get request

cdm014 I'm setting up a page where our staff can view a list of items that should be on the shelf and then confirm if those items are there, or raise an alert if something goes wrong. I'm trying to create a function for each case (item exists, item doesn't exi

jQuery Ajax not sending multiple data with post request

I am eating I am trying to send back an array or object using jquery post request. I have multiple inputs with the same name like: $.ajax({ type: "POST", url: "/details", date:{ number:$('input[name="number[]"]').val()

jQuery Ajax not sending multiple data with post request

I am eating I am trying to send back an array or object using jquery post request. I have multiple inputs with the same name like: $.ajax({ type: "POST", url: "/details", date:{ number:$('input[name="number[]"]').val()

jQuery Ajax not sending multiple data with post request

I am eating I am trying to send back an array or object using jquery post request. I have multiple inputs with the same name like: $.ajax({ type: "POST", url: "/details", date:{ number:$('input[name="number[]"]').val()

jQuery Ajax not sending multiple data with post request

I am eating I am trying to send back an array or object using jquery post request. I have multiple inputs with the same name like: $.ajax({ type: "POST", url: "/details", date:{ number:$('input[name="number[]"]').val()

jQuery Ajax PUT request not sending data to server

hushan roda I have an ajax put request that doesn't send data to the server handleUpdate(meal) { console.log(meal) var meals = this.state.meals; $.ajax({ url: `api/v2/meals/${meal.id}`, type: 'PUT', date: {name: meal.name, calories: meal.calories, meal_t

Sending data using POST via AJAX and PHP

Benjamin Oatmeal I have an object that I want to send in an AJAX request: function send_value() { $.ajax({ type: 'post', url: 'get.php', data: { source1: "some text", source2: "some text 2", uniI

Error sending data to php using Ajax in Wordpress

Bruno I have this form: <select id="mudar_produto"> <option></option> <option value="#produto_1">Novo Produto Higiene</option> </select> <section class="hide-section" id="produto_1"> <form class="form-validate" id="feedback_form"> <div class="ca

Error sending JSOn data to PHP via Ajax

User 123 I am able to send the JSON data to the server via Ajax as I can see it in the parameters of My Browser Developer Tool => Network but I get no response and even trying to do this Print $_REQUESTI $_POSTjust get the Cookie value instead of me data sent

PHP returns no data when sending ajax request

beans, lentils When I send an ajax post request to my getMessages.php file it returns nothing. I've tried manually setting the array values and printing them in the console and that seems to work. get message.php <?php require_once "mysqli.php"; $data = arra

No data response when sending ajax request to php

Markop I want to send a json data to my php but when i access it to my php there is no response. this is my ajax request var project = {project:"A"}; var dataPost = JSON.stringify(project); $.ajax({ url: 'fetchDate.php', data: {myData: da

Issue with sending data to php script using ajax

Andrew I'm having trouble sending form data to a PHP script via Ajax. I get this error when sending data Fatal error: Class Libs\Controller not found I have written my own MVC project structure and it works fine if I only send data using POST request but when

Sending data using POST via AJAX and PHP

Benjamin Oatmeal I have an object that I want to send in an AJAX request: function send_value() { $.ajax({ type: 'post', url: 'get.php', data: { source1: "some text", source2: "some text 2", uniI

Sending data using POST via AJAX and PHP

Benjamin Oatmeal I have an object that I want to send in an AJAX request: function send_value() { $.ajax({ type: 'post', url: 'get.php', data: { source1: "some text", source2: "some text 2", uniI

No data response when sending ajax request to php

Markop I want to send a json data to my php but when i access it to my php there is no response. this is my ajax request var project = {project:"A"}; var dataPost = JSON.stringify(project); $.ajax({ url: 'fetchDate.php', data: {myData: da