繁体   English   中英

数据通过 Ajax 请求发送,但 PHP 脚本未接收

[英]Data is sent through Ajax request but PHP script is not receiving

我正在尝试将表单数据发送到 php 脚本,但该脚本没有接收到数据。
我不明白为什么,Javascript 应该没问题 php 应该没问题。
在 ajax 请求之前测试数据显示结果,但从服务器端检查 POST 数据返回空。

这是我的 ajax 代码:

var request = $.ajax({
        method: "POST",
        url: 'contact-mail.php',
        data: { nume: nume.value, telefon: telefon.value, email: email.value, message: message.value},
        error: function(err, status) { console.log("error contact " + err);}
    });
    request.done(function(msg) {
        alert(msg);
        document.querySelectorAll(".sdc-cardboard-form input").forEach(input => {
            input.value = "";
        });

    });
    request.fail(function( jqXHR, textStatus ) {
        alert( "Request failed: " + textStatus );
    });

这就是我在 PHP 脚本中获取数据的方式:

$name = $_POST['nume'];
$name = addslashes($name);
$tel = $_POST['telefon'];
$tel = addslashes($tel);
$email = $_POST['email'];
$email = addslashes($email);
$message = $_POST['message'];
$message = addslashes($message);

echo "Server side: ".$name." ".$tel." ".$email." ".$message;

Echo 仅返回“服务器端:”部分。

我正在使用相同的方法将数据发送到另一个网站上的服务器。
有人看到我的代码中有任何错误吗? 有什么我想念的吗?
Jquery 3+
PHP 7.2

更新,问题。 会不会是 htaccess 重写规则,它会从.php 开始修剪所有内容?

这是我的 htaccess 重写规则:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

我没有阅读太多关于 htacess 的内容,所以我只是在猜测。

由于您使用 mod-rewrite 来重定向文件扩展名,因此您需要在 AJAX 请求中执行此操作。 改变:

url: 'contact-mail.php', 

url: 'contact-mail', 

Due to .htaccess rewrite rules (Apache mod_rewrite: https://httpd.apache.org/docs/2.4/rewrite/intro.html ) all the url for those PHP files will be update

表示example.com/test.php将是example.com/test

这就是为什么您的 AJAX 请求没有找到 URL 并给您一个错误的原因。

url: 'contact-mail.php'

应该是

url: 'contact-mail'

请检查这是否有效。 如果它不起作用,请回复或评论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM