【问题标题】:ajax post method unable to recieved data in php fileajax post方法无法接收php文件中的数据
【发布时间】:2016-07-12 16:44:19
【问题描述】:

forget.phpPHP:

if (! (empty($_POST['emailforget'])) ) {
    echo "here in the function";
} else {
    echo "here";
}

AJAX:

$("#passreset").on('click', function(e) {

  var emailforget = $("#tempemail").val();
  alert(emailforget);
  //ajax call here
  $.ajax({
    type: "post",
    url: 'user/forgetpass.php',
    data: {
      'emailforget': emailforget
    },
    cache: false,
    contentType: false,
    processData: false,
    beforeSend: function() {
      //alert("here in function beforesent");
    },
    success: function(html) {
      //alert(html);
      document.getElementById("error").innerHTML = html;
    },
    error: function() {
      alert("alert error");
    }

  });

  return false;


});

HTML:

<input type="email" id="tempemail" name="tempemail" 
    placeholder="Enter your email address" value="ab.waseem@yahoo.com" required>

代码运行良好,但突然停止工作。

在 Firefox 和 Chrome 控制台中检查没有错误。

调用完美地发送到相应的文件。

【问题讨论】:

  • 使用您的开发者控制台(Chrome 或 FF)并单击“网络”选项卡,然后限制为“XHR”——这是您的 AJAX 请求。触发 AJAX 调用。检查电话 - 是 200 吗?如果是这样,服务器返回的响应是什么? (这一切都在开发者控制台中可用:developer.chrome.com/devtools)。通话是否发生?如果不是,那么您的 jQuery 是否在文档的 head 中?如果是这样,则 jQuery 加载时 passreset 元素不存在,因此请参阅此问答:stackoverflow.com/questions/6173238/jquery-event-not-firing
  • 是否可以像您的 URL 指向 user/forgetpass.php 而您的实际文件为 forget.php 一样简单? (根据您的问题元信息推断)
  • @cale_b 是的 ajax 调用是 200,但它没有传递数据 ($_POST['emailforget']) 总是空的..
  • 另外,如果你使用 jQuery,像这样:document.getElementById("error").innerHTML=html; 可能变成 $('#error').html(html);....
  • &lt;form&gt; 的其余部分是什么样的?

标签: javascript jquery ajax


【解决方案1】:

问题是processData 是假的。只需完全删除此选项,它应该可以工作。如果这不起作用,请同时删除 contentType

有关详细信息,请仔细阅读options 上的文档。

processData 设置为false 意味着以创建的形式发送数据。设置为true,它将把它处理成一个查询字符串。

默认的内容类型标头是application/x-www-form-urlencoded; charset=UTF-8,将内容类型设置为false 同样会完全删除它。

除非您确切知道为什么首先需要更改这些默认设置,否则我不会修改这些默认设置。

【讨论】:

  • 有关为什么更改 processData 会有所作为的更多详细信息,请参阅此答案:stackoverflow.com/a/6253318/870729
  • @TalhaAslam 不客气 :) 请不要忘记在宽限期到期时接受答案~
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多