0

I am doing ajax call. And every time it is failing with occurring a 500 Internal server error. But no error in the client side code.

JavaScript code:

$.ajax({
    url:"test.php",
    type:"POST",
    dataType:"html",
    data:{
        userInput:userInput /* userInput is some text value */
    }
});

PHP code:

<?php
    $con=mysqli_connect("localhost","root","password","test");
    $user_data=$_POST['userInput'];
    echo $user_data;
?>

every time I see these error codes in console:

POST XHR http://localhost/test.php and [HTTP/1.0 500 Internal Server Error 1ms]

Ask me if you need more informations.

NB:I found many questions in this community. None of those solved my problem.

partho
  • 1,101
  • 2
  • 21
  • 37
  • 1
    Where do you get the idea from that there's a 500 error? It quite clearly states _404 Not Found_, which would suggest the url (localhost/test.php) is incorrect. Also try adding a success and error callback to the `$.ajax` call, so you can log more info there – Elias Van Ootegem Sep 29 '15 at 16:20
  • Are you sure that `test.php` is actually located at the root of server? – hjpotter92 Sep 29 '15 at 16:20
  • Are you sure both test.php and the file in which you use ajax are in same directory? – Shahzad Barkati Sep 29 '15 at 16:21
  • `test.php` and `test.html` are in same directory – partho Sep 29 '15 at 16:22
  • Maybe do you have set some rule at the RewriteEngine? – kosmos Sep 29 '15 at 16:22
  • Verify the method is POST our is GET in php app. – Emir Marques Sep 29 '15 at 16:22
  • I din't create htaccess file – partho Sep 29 '15 at 16:25
  • 1
    @EmirMarques: It's post, that's clear when looking at the code. To the OP: what error are you actually getting? It was 404 originally, you've now changed it to 500. Which on is it? And again: add a success/error callback, and do a simple `console.log(arguments)` and look at what's being passed to those functions – Elias Van Ootegem Sep 29 '15 at 16:25
  • I see now the error code is 500, so check out your `php_error_log` file for errors (or add a callback as @EliasVanOotegem proposed). – kosmos Sep 29 '15 at 16:27
  • @EliasVanOotegem, once I got 404 error, but currently I get 500 error – partho Sep 29 '15 at 16:30
  • FWIW: Firebug tends to give you a lot more information on ajax errors than chrome or IE (if you aren't using it already) – disappointed in SO leadership Sep 29 '15 at 16:36
  • `error:function(jqXHR){console.log(jqXHR);}` gives this error: Object { readyState: 4, getResponseHeader: .ajax/v.getResponseHeader(), getAllResponseHeaders: .ajax/v.getAllResponseHeaders(), setRequestHeader: .ajax/v.setRequestHeader(), overrideMimeType: .ajax/v.overrideMimeType(), statusCode: .ajax/v.statusCode(), abort: .ajax/v.abort(), state: .Deferred/d.state(), always: .Deferred/d.always(), then: .Deferred/d.then(), 11 more… } – partho Sep 29 '15 at 16:37
  • SOLVED. It was a syntax error, But I was surprised that, it was showing internal server error!! – partho Sep 29 '15 at 17:25
  • 1
    @partho: A syntax error? Next time: please post the _actual_ code you're using, most of us would've been able to spot that quite quickly. And it's no surprise that a syntax error returns a 500 status: invalid syntax means php probably gave a fatal error, because it was unable to run the invalid code. That, in my book, is an internal server error – Elias Van Ootegem Sep 29 '15 at 17:27
  • @EliasVanOotegem, i am sorry. And thanks for the suggestion. – partho Sep 29 '15 at 18:04

2 Answers2

2

I think the internal server error is due to the mysqli_connect function call with mysqli extension not enabled. Try phpifo() to verify mysqli extension is enabled

Shanavas M
  • 1,581
  • 1
  • 17
  • 24
  • Client API library version 5.5.44 Active Persistent Links 0 Inactive Persistent Links 0 Active Links 0 Client API header version 5.5.44 MYSQLI_SOCKET /var/run/mysqld/mysqld.sock Directive Local Value Master Value mysqli.allow_local_infile On On mysqli.allow_persistent On On mysqli.default_host no value no value mysqli.default_pw no value no value mysqli.default_socket /var/run/mysqld/mysqld.sock /var/run/mysqld/mysqld.sock mysqli.default_user no value no value mysqli.max_links Unlimited Unlimited mysqli.max_persistent Unlimited Unlimited mysqli.reconnect Off Off – partho Sep 29 '15 at 16:45
  • you suspected right. commenting out `mysqli_connect` i see no error. `mysqli` isn't enabled. I am trying to enable it. Help me please? Using `ubuntu 14.04 LTS` – partho Sep 29 '15 at 17:16
  • 1
    Find answer here http://stackoverflow.com/questions/10769148/extension-mysqli-is-missing-phpmyadmin-doesnt-work – Shanavas M Sep 29 '15 at 17:29
0

You can catch your error executing a callback .. example:

$.ajax({
    url:"test.php",
    type:"POST",
    dataType:"html",
    data:{
        userInput:userInput /* userInput is some text value */
    }
}).success(function() {
    window.alert('Ajax completed successfully');
}).error(function() {
    window.alert('An error has occurred');
});