1

I have a small php server that i developed. The server waits to serve json files that will be sent to him.

I created an html page and i am using jquery to try send a json file to my server.

I have a button and when i press it :

$("button").click(function(){

                     var data = '{"deviceUUID":"25f998", "os":"bb", "pushToken":"l1355436gdfsfaddsl"}';
                     alert("User: " + userId + "\n" + "Data: " + data);

                     $.ajax({
                         type: "POST",
                         url: "http://192.148.2.123/Server_CityInfo/register.php",
                         data: data,
                         contentType: "application/json; charset=utf-8",
                         dataType: "json"
                         });


                 });

However the json file that arrives in the server is empty.. I know the server works , cause i have done post requests to it through java , obj-c , c# ...

What could be the problem here? The data variable on alert prints : {"deviceUUID":"25f998", "os":"bb", "pushToken":"l1355436gdfsfaddsl"} which is in the right json format , that my server accepts.

THIS IS MY SEVER SIDE , PHP

// We use php://input to get the raw $_POST results.
$json = file_get_contents('php://input');
$json_post = json_decode($json, true);

//creating variables from received json
$deviceUDID = $json_post['deviceUUID'];
$os = $json_post['os'];
$pushToken = $json_post['pushToken'];

So when i try to print the deviceUUID , or os or pushToken they are empty. In my database , that they are automatically inserted they appear as NULL . That means that the post request arrives , but empty... or in wrong format..

  • Any error on console ? – Adil Shaikh May 17 '13 at 15:20
  • what console , i just run it on my browser? How can i debug this thing? The request is made to my server. But the server says that the deviceUUID , the os and the pushToken it received were all NULL. So i guess i dont send the json file as i think , or is in wrong format – user2394470 May 17 '13 at 15:21
  • Yes browser console : `ctrl + shift + J ` – Adil Shaikh May 17 '13 at 15:23
  • add a `success` parameter to the ajax request, something simple like an `alert`, so you know if it returns or not. You might also want to add `dataType:"json"` as a parameter as well. – Josh Balcitis May 17 '13 at 15:24
  • the browser console doesnt show errors , i have the dataType:"json" cant you see it? And i didnt understand the success part. – user2394470 May 17 '13 at 15:29
  • did you try to post your data as js object, instead of a string : var data = {"deviceUUID":"25f998", "os":"bb", "pushToken":"l1355436gdfsfaddsl"}; ? Its the way I use $.post – oinant May 17 '13 at 15:38
  • if i do that , data prints me : [object Object] but sadly is empty again on the server – user2394470 May 17 '13 at 15:43

2 Answers2

0

Display the console depend on your browser. Chrome/ff : Ctrl + shift + i IE : f12 then click refresh

for the success part, he means use a callback once ajax call is finished. You can use either done or fail or allways :

$.ajax({
     type: "POST",
     url: "http://192.148.2.123/Server_CityInfo/register.php",
     data: data,
     contentType : "application/json; charset=utf-8",
     dataType: "json"
}).allways(function(response, status, xhr){
     console.log(response);
     console.log(status);
     console.log(xhr);
});

You will se the result of your call in the console.

TCHdvlp
  • 1,334
  • 1
  • 9
  • 15
  • Timestamp: 5/17/2013 5:59:49 PM Error: The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol. Timestamp: 5/17/2013 6:01:29 PM Error: TypeError: $.ajax(...).allways is not a function – user2394470 May 17 '13 at 16:00
  • WUT ? Ok, let me edit my answer, but I have never seen it before. – TCHdvlp May 17 '13 at 16:01
  • I fixed the error from here: http://stackoverflow.com/questions/11996257/the-character-encoding-of-the-html-document-was-not-declared i just added the 2 meta lines. Now i dont get any error and still the server receives empty json :p – user2394470 May 17 '13 at 16:09
  • if you delete the `dataType : 'json'` and put quotes arround your data variable. You agree you are sending a simple String to your server ?! Does it read this string or it is null too ? – TCHdvlp May 17 '13 at 22:26
0

On the following line, you are setting data to a string value:

var data = '{"deviceUUID":"25f998", "os":"bb", "pushToken":"l1355436gdfsfaddsl"}';

If you want this to be a Javascript Object (JSON), you need to remove the single quotes:

var data = {"deviceUUID":"25f998", "os":"bb", "pushToken":"l1355436gdfsfaddsl"};

Check out http://www.json.org/ for more infomation. This website has lots of information and tools on the JSON format.

Matthew
  • 265
  • 4
  • 10
  • I did it. See my comments after my question , when someone already mentioned that and i answered the new problem – user2394470 May 17 '13 at 16:05