PHP does not recognize Json code

Asked

Viewed 611 times

1

I installed php + apache via Xampp. PHP is working, but it doesn’t recognize Json, anyone knows if they need to enable anything in php.ini?

Returns the error: Notice: Undefined index: jsoncallback

The return line is:

echo $_GET["jsoncallback"] . '(' . json_encode("Valor=" . $teste ) . ');';    

The code works, I’ve run the same code on the localhost of another computer, and also on a remote server, and it works perfectly, only on this computer that I installed via Xampp does not work.

Thank you

1 answer

1

The mistake you’re getting:

Notice: Undefined index: jsoncallback

Indicates that the index in the matrix $_GET with the name jsoncallback there is no.

When you do:

echo $_GET["jsoncallback"];

You’re trying to access a matrix input that doesn’t exist.

To better control this you must make use of the function isset() of PHP:

if (isset($_GET["jsoncallback"])) {
  echo $_GET["jsoncallback"];
}
else {
  echo "Não foi localizado: jsoncallback";
}

Note: You should check if on the page address you have a URL parameter with the name jsoncallback.

Reserved Variable GET - PHP

An associative array of variables passed to the Current script via the URL Parameters.

That translated:

An associative array of variables passed to the current script through the URL parameters.

  • The problem is not in the code, as I said, it works perfectly on two servers, only in this one I installed now via Xampp that occurs this error, it is apache or php configuration.

  • I just tested on another computer now. The return when running php does not generate error. It makes the return of Json perfect. What may be missing from the other computer?

  • @user3771516 In your php.ini by chance check if you have the option cgi.force_redirect = 1. If yes, the problem may come from there, you should comment on that line and test again. (I no longer remember if it is necessary to restart, but due to doubts, restarts.)

  • @user3771516 Note that JSON may be coming up with problems just because you have a Notice PHP that is sent together with JSON resulting in a failure to read it. You can also tell PHP not to display Notice although the best thing to do is what I put in the answer. Put error_reporting(0); at the beginning of your PHP file and should turn off notice.

  • Now I am on a computer that does not generate the error. In ini, the cgi.force_redirect line = 1 was commented. I removed the comment from the line, and restarted Apache, but nothing has changed. Here everything is still running. If I can simulate the fault here, I discover the problem on the other computer. Any more ideas?

  • @user3771516 1. Shuts down Notice as I mentioned earlier and forehead. 2. Comment on the line you mentioned because it is supposed to be commented. 3. Use the browser inspector to see what is returned to you, if it is the Notice and then JSON is because everything is working as expected but the Notice is breaking your output, turning it off should be ok (ps: of course you are testing on the machine with the problem).

  • I did some more tests, but it continues with the message. Apparently Json is running, but opening the . php, returns errors as reported above. The code used is in the post http://answall.com/questions/22497/passar-valores-para-arquivo-php-com-json

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.