0

I'm trying to pass a simple stringified int array into a json exression and sent through ajax. The request url and headers all look fine to me going from client to server, but in Cake the request->query vardumps empty for some reason.

Headers from ajax:

Request URL:http://localhost/cakephp-2.2.5/paragraphs/sortPublished.json?[%221%22,%223%22,%222%22]
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:fi-FI,fi;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Type:application/json; charset=utf-8
Cookie:CAKEPHP=gu46lhu6lohe1hnr4vmeve23g1
DNT:1
Host:localhost
Referer:http://localhost/cakephp-2.2.5/paragraphs/edit/1
User-Agent:Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
X-Requested-With:XMLHttpRequest
Query String Parametersview sourceview URL encoded
["1","3","2"]:
Response Headersview source
Connection:Keep-Alive
Content-Length:425
Content-Type:application/json; charset=UTF-8
Date:Mon, 13 May 2013 09:11:14 GMT
Keep-Alive:timeout=5, max=98
Server:Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
X-Powered-By:PHP/5.4.7

And the Javascript

$(document).ready(function(){
$("#button").click(function() {
    var sortedParagraphs = $( "#sortable1" ).sortable('toArray');

    var key_value_pairs = [];
    var i = 0;
    $.each( sortedParagraphs, function(index, value) {
        key_value_pairs[index] = value;
    });

    $.ajax({
      url: define1,
      data: JSON.stringify(sortedParagraphs),
      dataType: "json",
      contentType: 'application/json; charset=utf-8',
      success: function(data) {
        alert(data);
      }
    });
});
});

For my amateur self it would appear clear that the JSON is formed correctly and I can't see any problems with the url, but request->query still produces:

array(0) {
}

I've been wrestling with this tiny problem since forever it feels, and any help would be appreciated.

1 Answer 1

1

First of all, you don't need to 'stringify' the variable, jQuery will already take care of that;

Because you're stringify'ing the array, jQuery sends a literal string-presentation of your array; i.e. '["1","3","2"]'. Basically, the name of your query parameter is '["1","3","2"]' and the value is not set.

Next, because you're not assigning a 'name' to the data you're sending, CakePHP will have no query parameters to read.

try this;

$.ajax({
   url: define1,
   data: {paragraphs: sortedParagraphs},
   dataType: "json",
   contentType: 'application/json; charset=utf-8',
   success: function(data) {
     alert(data);
   }
 });

And inside your controller;

debug($this->request->query);

Should output something like

array(
    'paragraphs' => array(
        (int) 0 => '1',
        (int) 1 => '3',
        (int) 2 => '2'
    )
)

Additional simplification

Since $( "#sortable1" ).sortable('toArray'); already returns an array, you don't have to manualy convert the results to an array (using $.each(...), so this will probably result in exactly the same:

$("#button").click(function() {
    $.ajax({
       url: define1,
       data: {paragraphs: $("#sortable1").sortable('toArray')},
       dataType: "json",
       contentType: 'application/json; charset=utf-8',
       success: function(data) {
         alert(data);
       }
     });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much! I've been twiddling with this thing for so long the iterations are starting show as incoherent pointless code, but until now without any results. Thank you thaJeztah, you're the ultimate coding friend:)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.