[英]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. 我正在尝试使用Redis,并希望构建一个简单的前端/后端设置来测试它并进行练习。 The front end is HTML / Javascript / JQuery and the back end is PHP / Apache / Redis.
前端是HTML / Javascript / JQuery,后端是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.
基本上,我想向后端发送一个帖子请求并收到一个响应,然后我会在Web控制台上显示。 To send the request, I used JQuery:
要发送请求,我使用了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. 我在Firefox上使用Firebug来查看实际情况。 In Firebug, I see a GET request being fired instead of a POST.
在Firebug中,我看到一个GET请求被触发而不是POST。 The jsoncallback string attached to the url might be the cause, but without it, I don't get a response at all.
附加到url的jsoncallback字符串可能是原因,但没有它,我根本得不到响应。 As a side note, I am expecting json back in response.
作为旁注,我期待json回应。 Here is my PHP code:
这是我的PHP代码:
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? jsoncallback的东西是获得响应的唯一方法,但是如何在没有它的情况下成功启动真正的POST请求?
It's probably because you are using jsonp as dataType, jsonp doesn't support POST requests. 这可能是因为您使用jsonp作为dataType,jsonp不支持POST请求。
Edit: Try sending your jsoncallback parameter, with the other parameters: 编辑:尝试使用其他参数发送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");}
});
It would appear that adding this line to my php file fixed the issue: 似乎将此行添加到我的php文件修复了该问题:
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.
我也知道有一种方法可以过滤这个,这样只有某些网站才能发出请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.