【问题标题】:JQuery ajax post is empty on php remote serverphp远程服务器上的JQuery ajax帖子为空
【发布时间】:2019-12-30 08:33:20
【问题描述】:

当我在本地 php 服务器上运行 ajax POST 请求时,$_POST 数组被填充。在远程服务器上,如果我使用 google chrome、MS Edge,则 $_POST 数组为空;但 $_POST 在 Firefox 上不是空的。

当我调查 HTTP 远程地址标头时,我注意到以下差异 在 chrome 中 --- 远程地址:95.168.185.183:8080 在firefox中---远程地址:185.27.134.216:80

如果将 ajax 调用改为 get 类型,代码运行良好。

我的 ajax 调用如下所示:

$.ajax({
  url: 'index.php/designer/ajax_add_template_to_cart',
  type: 'post',
  datatype:'json',

  data:{
    template_name : template_name,
    size:size,
    qty: 1
  },

   error: function(data) {
    console.log(data);
   },

  success: function(data){
    console.log(data);
  },
});

【问题讨论】:

  • 这个问题似乎不太可能源于上述 javascript 代码
  • 似乎在所有具有代理连接的浏览器中:保持活动请求标头,$_POST 为空,这些浏览器是基于铬的浏览器和 MS Edge,但适用于 android 的 firefox 和 chrome 没有该标头和一切正常。

标签: php ajax google-chrome firefox


【解决方案1】:
//You must use valid URL
$.ajax({
url: 'index.php/designer/ajax_add_template_to_cart',
type: 'POST',
data: jQuery.param({ template_name: template_name, size: template_name ,qty:1}) ,
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
success: function (response) {
    alert(response.status);
},
error: function () {
    alert("error");
}
}); 

【讨论】:

  • 谢谢...但是 $_POST 是空的,当我使用开发人员工具来插入请求标头数据时,所有浏览器都在发送...但是服务器似乎只接收来自 firefox 的数据,和 chrome android
【解决方案2】:

您可以尝试在 AJax 请求中更改代码序列吗?

$.ajax({
  url: 'index.php/designer/ajax_add_template_to_cart',
  type: 'POST',
  data:{
    template_name : template_name,
    size:size,
    qty: 1
  },
  dataType:"JSON",
  success: function(data){
    console.log(data);
  },
   error: function(data) {
    console.log(data);
   },
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多