[英]Ajax and PHP, post request not working
So I am trying to post some some data from one PHP file to another PHP file using jquery/ajax. 所以我试图使用jquery / ajax将一些数据从一个PHP文件发布到另一个PHP文件。 The following code shows a function which takes takes data from a specific div that is clicked on, and I attempt to make an ajax post request to the PHP file I want to send to.
以下代码显示了一个函数,该函数从单击的特定div中获取数据,然后尝试向要发送到的PHP文件发出ajax发布请求。
$(function (){
$(".commit").on('click',function(){
const sha_id = $(this).data("sha");
const sha_obj = JSON.stringify({"sha": sha_id});
$.ajax({
url:'commitInfo.php',
type:'POST',
data: sha_obj,
dataType: 'application/json',
success:function(response){
console.log(response);
window.location.replace("commitInfo");
},
error: function (resp, xhr, ajaxOptions, thrownError) {
console.log(resp);
}
});
});
});
Then on inside the other php file 'commitInfo.php' I attempt to grab/print the data using the following code: 然后在另一个php文件'commitInfo.php'中,我尝试使用以下代码来获取/打印数据:
$sha_data = $_POST['sha'];
echo $sha_data;
print_r($_POST);
However, nothing works. 但是,没有任何效果。 I do not get a printout, and the $_POST array is empty.
我没有得到打印输出,并且$ _POST数组为空。 Could it be because I am changing the page view to the commitInfo.php page on click and it is going to the page before the data is being posted?
可能是因为单击时我将页面视图更改为commitInfo.php页面,并且要在发布数据之前转到该页面吗? (some weird aync issue?).
(一些怪异的aync问题?)。 Or something else?
或者是其他东西? I have tried multiple variations of everything yet nothing truly works.
我尝试了所有方法的多种变体,但没有真正起作用。 I have tried using 'method' instead of 'type', I have tried sending dataType 'text' instead of 'json'.
我尝试使用“方法”而不是“类型”,我尝试发送数据类型“文本”而不是“ json”。 I really don't know what the issue is.
我真的不知道问题是什么。 Also I am running my apache server on my local mac with 'sudo apachectl start' and running it in the browser as ' http://localhost/kanopy/kanopy.php ' && ' http://localhost/kanopy/commitInfo.php '.
另外,我还在本地Mac上使用“ sudo apachectl start”运行apache服务器,并在浏览器中以“ http://localhost/kanopy/kanopy.php ”和&“ http://localhost/kanopy/commitInfo.php运行它'。
Also, when I send it as dataType 'text' the success function runs, but I recieve NO data. 另外,当我将其发送为dataType'text'时,成功函数会运行,但是我没有收到任何数据。 When I send it as dataType 'json' it errors.
当我将其作为dataType'json'发送时,它出错。 Have no idea why.
不知道为什么。
If anyone can help, it would be greaat! 如果有人可以帮助,那就太好了!
You don't need to JSON.stringify
, you need to pass data as a JSON object: 您不需要
JSON.stringify
,您需要将数据作为JSON对象传递:
$(function() {
$(".commit").on('click', function() {
const sha_id = $(this).data("sha");
const sha_obj = {
"sha": sha_id
};
$.ajax({
url: 'commitInfo.php',
type: 'POST',
data: sha_obj,
dataType: 'json',
success: function(response) {
console.log(response);
},
error: function(resp, xhr, ajaxOptions, thrownError) {
console.log(resp);
}
});
});
});
And on commitInfo.php
, you have to echo
string on json
format 在
commitInfo.php
,您必须以json
格式echo
字符串
===================================== =====================================
If you want to redirect to commitInfo.php
you can just: 如果要重定向到
commitInfo.php
,可以执行以下操作:
$(".commit").on('click',function(){
const sha_id = $(this).data("sha");
window.location.replace("commitInfo.php?sha=" + sha_id );
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.