php - Unable to retrieve json data on server side via ajax call

437

This is my Ajax call which I have copy pasted from a script

       $.ajax({

        type: "POST",
        url: "createpdf.php?v=635473328964700000",
        data: optionsJSON,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        beforeSend: calls.beforeSend,
        success: calls.success,
        error: calls.error,
        complete: calls.complete

    });

When I inspect my console in firefox

In params tab it says

v 635473328964700000

In POST tab it says

  {'path':'services/imgProcess.php?c=ixs&t=rt7&ap=0&bp=0&cp=0&a1=01&a2=01&a3=01&     a4=01&a5=01&a6=01&a7=01&a8=01&a9=01&a10=01&a11=01&a12=01&a13=01&a14=01&a15=01&a16=01&a17=01&a18=01&a19=01&a20=01&b1=01&b2=01&b3=01&b4=01&b5=01&b6=01&b7=01&b8=01&b9=01&b10=01&b11=01&b12=01&b13=01&b14=01&b15=01&b16=01&b17=01&b18=01&b19=01&b20=01&c1=none&c2=none&c3=none&c4=none&c5=none&c6=none&c7=none&c8=none&c9=none&c10=none&c11=none&c12=none&c13=none&c14=none&c15=none&c16=none&c17=none&c18=none&c19=none&c20=none&l=1&acc-kitr=0&acc-kitm=0&acc-additionalkit=additionalkit&acc-plasticprotection=0&acc-fingerprotection=0&acc-knuckleprotection=0&acc-ballsthumb=0&width=720&height=480&cache=1411735356642','accessories':'{

  "sliders": "0",
  "kitr": "0",
  "kitm": "0",
  "additionalkit": "additionalkit",
  "plasticprotection": "0",
 "fingerprotection": "0",
 "knuckleprotection": "0",
 "ballsthumb": "0"
  }'}

In Response it says Array() I have this code in my createPDF.php file

 print_r($_POST);

How I can retrieve posted data on server side. Please help me this is very confusing..

972

Answer

Solution:

try this

  $.ajax({

    type: "POST",
    url: "createpdf.php?v=635473328964700000",
    data: optionsJSON,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success:function(data){
        console.log(data);

        //json parse
        var obj = JSON.parse(data);
        console.log(obj);

      }
    });

Now you can access the json data by using obj object e.g. var name = obj.name;

464

Answer

Solution:

Currently you are sending data in json format, so php cannot parse the data into the$_POST array. Either change your ajax to send post data:

data: {options: optionsJSON},
//contentType: "application/json; charset=utf-8",

or change your php to access the raw post stream

print_r(file_get_contents('php://input'));

People are also looking for solutions to the problem: PHP gmdate find out if it's been a year

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.