1

I am trying to pass a jquery string to my cakephp controller but I keep getting an error saying json_decode expects a string but is passed an array.

Here is the code for my ajax:

var str = 'Data that i need';

    var jsonString = JSON.stringify(str);

    $.ajax({
        type: "POST",
        url: "http:/Configs/",
        cache: false,
        datatype: 'json',
        data: {data : jsonString},
        timeout: 100000,

        success: function(data) 
        {
            alert('Ok');
        }
    });

and in my controller im trying to recieve the data like so:

$value = json_decode($_POST['data']);

but im getting the error stated above any idea how to solve this would be great thanks.

Schokea
  • 708
  • 1
  • 9
  • 28
  • fyi - htt://Configs, not http:/Configs – Seth Flowers Aug 21 '12 at 13:03
  • Why what difference does it make? – Schokea Aug 21 '12 at 13:16
  • Oops, meant http://. I was just pointing out the double slash. I'm not sure the answer as to why. It's part of RFC 3986, and is required if there is an authority (?)... you'll have to read the rfc for an explanation on that. http://tools.ietf.org/html/rfc3986#section-3. You can also see this [SO question](http://stackoverflow.com/questions/8870227/what-is-the-semantics-of-the-double-slash-following-the-scheme-in-a-uri). – Seth Flowers Aug 21 '12 at 13:33
  • Sorry I have double // in but i didn't want to post my server address up so i just deleted majority of address and obviously went one slash to far :) – Schokea Aug 21 '12 at 13:36

2 Answers2

1

try this:

var str = 'Data that i need';

var jsonString = JSON.stringify(str);

var DataToSend = {};
DataToSend.data = JSON.stringify(jsonString);

$.ajax({
    type: "POST",
    url: "http:/Configs/",
    cache: false,
    datatype: 'json',
    data: JSON.stringify(DataToSend),
    timeout: 100000,

    success: function(data) 
    {
        alert('Ok');
    }
});
Neb
  • 354
  • 2
  • 12
  • then it's in the backend.. that Jquery code is fine. In .NET, I can parse the json data. I don't know much in php – Neb Aug 21 '12 at 14:17
  • What do you mean by back end could you please elaborate? – Schokea Aug 21 '12 at 14:28
  • I mean the problem seem to be in the "backend" wich mean the code that run on the server. In your case, it's in the PHP. I took this Jquery code and I was able to parse it in .NET so it means that the Jquery code here is fine. I can't help you in PHP, sorry – Neb Aug 21 '12 at 14:57
1

Are you sure the url you use is correct? If you are using CakePHP, then why don't you work with $this->request->data?

Try to debug your code with

pr($_POST); exit;

or

pr($this->request); exit;

What do they say?

rookian
  • 1,045
  • 14
  • 38
  • Ye this has helped it gives me params] => Array ( [plugin] => [controller] => configs [action] => index [named] => Array ( ) [pass] => Array ( ) [isAjax] => ) [data] => Array ( [BEHIND_GATEWAY] => NO [DHCP_SERVER] => [DHCP_SERVER_IP] => 159. [DHCP_SERVER_ROOT_PASS] => 1 These are the values I need – Schokea Aug 21 '12 at 14:42