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?
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?
Share Improve this question edited Apr 1, 2014 at 23:17 Gregory-Turtle asked Apr 1, 2014 at 22:17 Gregory-TurtleGregory-Turtle 1,7173 gold badges20 silver badges31 bronze badges 5- Why did you set that get parameter at all? It doesn't seem needed in your script – Francisco Presencia Commented Apr 1, 2014 at 22:21
- it isn't possible to POST with the jsonp datatype. – Kevin B Commented Apr 1, 2014 at 22:23
- According to an answer I found on another question, the data returned from PHP has to be "wrapped" by the jsoncallback that is sent via get. Without all the jsoncallback stuff, the request is fired but nothing is returned – Gregory-Turtle Commented Apr 1, 2014 at 22:23
- I use "text" as datatype for my posts, but you might want json, instead of jsonp – Baked Inhalf Commented Apr 1, 2014 at 22:27
- I changed it to json, but it still changed into a GET request. – Gregory-Turtle Commented Apr 1, 2014 at 22:34
2 Answers
Reset to default 5It'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");}
});
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.