简体   繁体   中英

json request working in localhost but not in server

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....?

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

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM