AJAX POST request becomes a GET request

I tried Redis and wanted to create a simple front / back setting to test it and practice. The front end is HTML / Javascript / JQuery, and the back end is PHP / Apache / Redis. Basically, I wanted to send a request to send to the background and get a response, which I will then post on the web console. To send a query, 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 use Firebug in Firefox to find out what is really happening. In Firebug, I see a GET request that runs instead of POST. The jsoncallback string attached to the url may be the reason, but without it I don't get the answer at all. As a side note, I expect an answer in return. 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;
}

jsoncallback , POST- ?

+4
2

, , jsonp dataType, jsonp POST.

: jsoncallback :

$.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");}
        });
+5

, php :

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

, , . , . , , .

0

Source: https://habr.com/ru/post/1534543/


All Articles