Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

500 Internal Server, Unexpected token s in JSON at position 0 #158

Closed
Taremeh opened this issue Dec 16, 2016 · 10 comments
Closed

500 Internal Server, Unexpected token s in JSON at position 0 #158

Taremeh opened this issue Dec 16, 2016 · 10 comments
Assignees
Labels

Comments

@Taremeh
Copy link

@Taremeh Taremeh commented Dec 16, 2016

Hi,
I downloaded your repo and tried to use it. Unfortunately I'm getting an error (500 Internal Server).
Then I uncommented var_dump($_SERVER['REQUEST_METHOD'],$_SERVER['PATH_INFO']); die(); in the api.php and got the following error:

    VM2370:1 Uncaught SyntaxError: Unexpected token s in JSON at position 0
    at JSON.parse (<anonymous>)
    at XMLHttpRequest.xhttp.onreadystatechange (https://domain.com/api/examples/client.html:8:46)xhttp.onreadystatechange @ client.html:8

I think this has something to do with the JSON.parse (php_crud_api_transform(JSON.parse(this.responseText))) function.

Have a look: http://stackoverflow.com/questions/14432165/uncaught-syntaxerror-unexpected-token-with-json-parse

However, I don't know how to fix it... I'd really love to use php-crud-api. Therefore I attached a screenshot of my MySql-Database structure.

screenshot-db

I'd appreciate your help!
Thx

@mevdschee

This comment has been minimized.

Copy link
Owner

@mevdschee mevdschee commented Dec 17, 2016

Can you look at the response from the server (check the "net" tab in the debugger of the browser) and post the results?

@mevdschee mevdschee self-assigned this Dec 17, 2016
@Taremeh

This comment has been minimized.

Copy link
Author

@Taremeh Taremeh commented Dec 17, 2016

string(3) "GET"
string(10) "/composers"

bildschirmfoto 2016-12-17 um 13 44 08

(composers is the name of the table. )

Could the error be caused because of permission issues? (file rights, permissions, .htaccess or stuff like this?).

api.php > 644
client.php > 644
client.html > 644

This is my api.php at the bottom (values changed of course):

$api = new PHP_CRUD_API(array(
	'username'=>'user-name',
	'password'=>'password',
	'database'=>'database-name'
));
$api->executeCommand();

Thx for your fast answer, let me know if there's something else I could provide you.

@mevdschee

This comment has been minimized.

Copy link
Owner

@mevdschee mevdschee commented Dec 17, 2016

attached a screenshot of my MySql-Database structure.

I would put the collation at 'utf8mb4_general_ci'. Also you should have a primary key defined. Is 'id' a primary key?

(composers is the name of the table.)

The output is as expected and you should comment that line again. Your environment seems normal.

Could the error be caused because of permission issues?

Not very likely

When you have a 500 (Internal Server) error, then there is a line logged in your "error.log". You may find that file at /var/log/apache2/error.log depending on your hosting configuration.

Can you tail that file to retrieve the last lines and post the contents here?

@Taremeh

This comment has been minimized.

Copy link
Author

@Taremeh Taremeh commented Dec 17, 2016

I would put the collation at 'utf8mb4_general_ci'. Also you should have a primary key defined. Is 'id' a primary key?

Done. id is a primary key.

Here the error.log:

[Sat Dec 17 09:35:09.811625 2016] [:error] [pid 16679] [client 123.45.67.891:59662]
PHP Notice:  Use of undefined constant MYSQLI_OPT_INT_AND_FLOAT_NATIVE - assumed 'MYSQLI_OPT_INT_AND_FLOAT_NATIVE' in /var/www/html/api.php on line 109

[Sat Dec 17 09:35:09.811685 2016] [:error] [pid 16679] [client 123.45.67.891:59662] 
PHP Warning:  mysqli_options() expects parameter 2 to be long, string given in /var/www/html/api.php on line 109

[Sat Dec 17 09:35:09.811915 2016] [:error] [pid 16679] [client 123.45.67.891:59662] 
PHP Fatal error:  Uncaught exception 'Exception' with message 'Error setting int and float native. ' in /var/www/html/api.php:110\nStack trace:\n#0 /var/www/html/api.php(1836): 

MySQL->connect(NULL, 'username', 'password', 'database', NULL, NULL, 'utf8')\n#1 /var/www/html/api.php(2259): 
PHP_CRUD_API->__construct(Array)\n#2 {main}\n  thrown in /var/www/html/api.php on line 110
@Taremeh

This comment has been minimized.

Copy link
Author

@Taremeh Taremeh commented Dec 17, 2016

At this point I'm really exhausted right now... I played with every value, even setup various VPS multiple times to check that nothing is wrong with them... But still getting the same error...

I can't imagine what the problem is.

apache2 error.log from the latest try (no errors):

[Sat Dec 17 11:17:59.746557 2016] [mpm_prefork:notice] [pid 2210] AH00169: caught SIGTERM, shutting down

[Sat Dec 17 11:18:00.780635 2016] [mpm_prefork:notice] [pid 2269] AH00163: Apache/2.4.7 (Ubuntu) PHP/5.5.9-1ubuntu4.17 configured -- resuming normal operations

[Sat Dec 17 11:18:00.780714 2016] [core:notice] [pid 2269] AH00094: Command line: '/usr/sbin/apache2'

and console.log from client.html:

VM4936:1 Uncaught SyntaxError: Unexpected token s in JSON at position 0
    at JSON.parse (<anonymous>)
    at XMLHttpRequest.xhttp.onreadystatechange (http://123.123.123.1/examples/client.html:8:46)xhttp.onreadystatechange @ client.html:8
mevdschee added a commit that referenced this issue Dec 17, 2016
@mevdschee

This comment has been minimized.

Copy link
Owner

@mevdschee mevdschee commented Dec 17, 2016

@Taremeh Please don't give up. I know it is frustrating, but together we can figure this out.

Unexpected token s in JSON at position 0

As long as the script keeps outputing warning and errors the JSON will not be valid.

As you can see I updated the code for you. Please try the latest version, it should be compatible with older mysqlnd drivers that do not support 'MYSQLI_OPT_INT_AND_FLOAT_NATIVE' properly.

NB: It does mean that you will have integers as strings in your JSON (due to the old driver).

@mevdschee

This comment has been minimized.

Copy link
Owner

@mevdschee mevdschee commented Dec 17, 2016

Can you open the "net" tab of your browsers debugger and check the results of the AJAX call? It should contain something else than a JSON object. I added a 'console.log' line to the 'client.html' code for you, so it should be easy to find, see: 17a475e

mevdschee added a commit that referenced this issue Dec 17, 2016
@mevdschee

This comment has been minimized.

Copy link
Owner

@mevdschee mevdschee commented Dec 17, 2016

I did some fixes to improve PHP 5.3 support, see: 026f2a1

Please report whether or not this helps you.

@Taremeh

This comment has been minimized.

Copy link
Author

@Taremeh Taremeh commented Dec 17, 2016

@mevdschee Thank you so much! 🎉 The updated version finally works now (Ubuntu 14.04 with PHP 5.5.9)! I think the main problem was (as you said) the outdated version of PHP and Co.

I used Ubuntu 14.04 with PHP 5.5.9, not too old but I'll rethink whether I should use Ubuntu 16 and PHP 7 instead 😄

I really love your work and appreciate the support, keep it going! 👍

@Taremeh Taremeh closed this Dec 17, 2016
@mevdschee

This comment has been minimized.

Copy link
Owner

@mevdschee mevdschee commented Dec 17, 2016

Thank you for keeping faith. I really appreciate your contribution. I added some VMs using older Ubuntu versions to my test machines to prevent such issues from happening again. See: ae823a8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.