Gregory-Turtle
Gregory-Turtle

Reputation: 1717

Ajax POST request becomes a GET request

I am trying out Redis and wanted to build a simple front end / back end setup to test it and to practice. The front end is HTML / Javascript / JQuery and the back end is PHP / Apache / Redis. Basically, I wanted to send a post request to the back end and receive a response, that I would then display on the web console. To send the request, I used JQuery:

var data = $("#login_form :input").serializeArray();

var username = data[0]['value'];
var password = data[1]['value'];

$.ajax({
        type: "POST",
        url: "http://localhost/Convo/user.php?jsoncallback=%3F",
        dataType: "json",
        cache: false,
        data: { username: username, password: password, method: "create" },
        success: function(text){console.log("awesome");}
        });

I am using Firebug on Firefox to see what is really going on. In Firebug, I see a GET request being fired instead of a POST. The jsoncallback string attached to the url might be the cause, but without it, I don't get a response at all. As a side note, I am expecting json back in response. Here is my PHP code:

    error_reporting(E_ALL);
ini_set('display_errors', '1');
require 'Predis/Autoloader.php';
Predis\Autoloader::register();

$body = array();
$head = array();

$redis = new Predis\Client(array(
        'scheme' => 'tcp',
        'host'   => '127.0.1.1',
        'port'   => 6379,
));

if(!$redis)
{
    $body['status'] = "fail";
    $body['message'] = "unable to connect to database";
    $head['body'] = $body;

    header('Content-type: application/json');
    echo json_encode($head);
    exit;
}
else
{
    $body['status'] = "success";
    $body['message'] = "connected to database!";
    $head['body'] = $body;
    $jsoncallback = $_POST['jsoncallback'];

    header('Content-Type: application/json');
    echo $jsoncallback . '(' . json_encode($head) . ')';
    exit;
}

The jsoncallback stuff was the only way to get a response, but how do I successfully launch a true POST request without it?

Upvotes: 4

Views: 4328

Answers (2)

Gregory-Turtle
Gregory-Turtle

Reputation: 1717

It would appear that adding this line to my php file fixed the issue:

header('Access-Control-Allow-Origin: *');

I understand that this is not best practice but I was at the end of my rope. It was a cross site prohibition that prevented a response from returning to my front end. I am also aware that there is a way to filter this so that only certain sites can make requests.

Upvotes: 1

Vinicius Souza
Vinicius Souza

Reputation: 151

It's probably because you are using jsonp as dataType, jsonp doesn't support POST requests.

Edit: Try sending your jsoncallback parameter, with the other parameters:

$.ajax({
        type: "POST",
        url: "http://localhost/Convo/user.php",
        dataType: "json",
        cache: false,
        data: { username: username, password: password, method: "create", jsoncallback: "%3F" },
        success: function(text){console.log("awesome");}
        });

Upvotes: 5

Related Questions