【问题标题】:AJAX doesn't send post dataAJAX 不发送帖子数据
【发布时间】:2019-12-16 10:35:46
【问题描述】:

我有这个问题,我无法从 ajax 正在向其发送数据的 .php 获得任何响应。 它应该在 mysql 上运行插入,我尝试手动输入查询以查看它是否有效。

这是发送 ajax 的函数

function Continue(SLAatendida) {
var url = window.location.pathname;
var id = url.substring(url.lastIndexOf('/')+ 1);
    console.log(SLAatendida);
    console.log(id);
    $.ajax({
    type: 'POST',
    url: 'ws.php',
    data: { 
        'id': id,
        'sla_atendida': SLAatendida
    },
    success: function(msg){
        console.log(msg);
    }
    });

以及应该运行查询的 php 文件 (ws.php)

    <?php
session_start();

header('Access-Control-Allow-Origin: *');
header('Content-type: text/html; charset=UTF-8');

include('debug.php');
include('../antest/dbconnect.php');

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$debug_to_console("ws");

$id = $_POST["id"];
$SLAatendida = $_POST["sla_atendida"];

$sql_upd_sla = "INSERT INTO sla (id, sla_atendida) VALUES ('$id','$SLAatendida')";
$data = $conn->query($sql_upd_sla);
if($conn->query($sql_upd_sla))
{
    echo "Registro efetuado com sucesso";
} else {
    echo "Houve um erro";
    die();
}
?>

console.log 都从 var 返回正确的值。

【问题讨论】:

  • 你确定它没有发送它吗?提交时检查开发者控制台的网络选项卡,然后查看请求选项卡以查看其中的内容。还要在你的 PHP 中执行 var_dump($_POST); 以查看它包含的内容。如果一切都在那里,echo $sql_upd_sla; 看看里面有什么。
  • @aynber 是的,我没有发送它是错误的,检查了网络选项卡和 ws.php 在那里,但是 var_dumpecho 都尝试不返回任何值,我也注意到console.log(msg) 返回页面中的所有代码,这应该发生吗?
  • 不。它应该返回一条消息,而不是代码。见stackoverflow.com/questions/5121495/…
  • 是的,我运行了所有检查并且 php 安装正确,即使我使用的是 7.2,是的,没有使用短标签

标签: php jquery ajax forms post


【解决方案1】:

在 URL 属性中,您必须将 http://localhost 或您在本地机器中配置的 url + ws.php 放入

示例:

url: 'http://localhost/ws.php',

【讨论】:

  • 哦原来是这个问题,非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2021-05-02
  • 2014-10-10
  • 1970-01-01
  • 1970-01-01
  • 2013-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多