4

XAMPP 1.7.4 and if i run a php file with json encoded it works fine in localhost but if i migrate this to server the request receiving in the server is NULL, i don't know what is wrong and even for simple program its not working

This is my code

<?php header('Content-type:application/json; charset:utf-8');
if(isset($_GET['request']))
{

$str = $_GET['request'];
$arr=json_decode($str, true);


$username2 = $arr['username'];
echo json_encode($username2);
}
?>

This is my Request filename.php?request={username:kavi}

If i run this in localhost my output is kavi

If i run this in server my output is nothing

I checked by using var_dump() but its showing NULL

Can any one help me please....?

3
  • What does var_dump(json_decode('{username:kavi}', TRUE)); output on your localhost? Pretty sure it's NULL. Commented Oct 17, 2013 at 14:49
  • Please show us the Javascript which is sending the request to the server. Commented Oct 17, 2013 at 14:52
  • Ya its showing NULL and if i use var_dump($arr) it showing output as array(1) { ["username"]=> string(4) "kavi" } in localhost not in server Commented Oct 17, 2013 at 15:14

2 Answers 2

3

{username:kavi} is not JSON. Strings must be quoted with " characters. Test your JSON (better yet: don't handcraft it in the first place).

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Quentin,i am new to development but i tried with quotes (filename.php?request={"username":"kavi"}) but still its not working if you don't mind can you please write a simple program so that i can check with my server.
0

Check the true condition on $arr=json_decode($str, true); I had the same issue, the json was working for me on WAMP but wasn't working when I migrated the code on the server, having the following line of code :

echo json_encode($json,JSON_PRETTY_PRINT);

I removed the JSON_PRETTY_PRINT and left just echo json_encode($json); and worked for me...

Comments

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.