0

目前我正在通过 post 向 PHP 文件提交表单,唯一的问题是我无法提交到该 php 文件,因为 ajax 出了点问题。我的问题是,如果后端重写了 url,我可以提交到不同的 URL 吗?

当前页面 URL:-实际:http ://site.com/topic.php?id=1- 重新编写:http ://site.com/topic/1

想要的页面 URL:-实际:http ://site.com/comment.php

如何发送到 comment.php 文件?我认为 javascript 假设我正在尝试做 site.com/topic/comment.php

HTML:

            <form id="comment_on_topic" onsubmit="return sendComment();">
            <textarea id="topicreply" name="topicreply">Initial value.</textarea>
            <input type="submit" name="submit" value="submit" />
        </form>

Javascript:

function sendComment(){
        var $form = $('#comment_on_topic');
        var $inputs = $form.find("input, select, button, textarea");
        var serializedData = $form.serialize();
        $inputs.prop("disabled", true);
        /* GET FORM INPUTS & SERIALIZE */

        var http = new XMLHttpRequest();
        http.open("POST", '/comment.php');
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http.send(serializedData);
        /* AJAX REQUEST TO POST TO FILE */

        return false;
    }

PHP://根本没有被执行..(在comment.php中)

    <?php if($_SERVER['REQUEST_METHOD'] == "POST") : ?>
<?php
    require_once 'protected/connect.inc.php';
    require_once 'protected/functions.inc.php';


    $comment = sanitize($_POST['topicreply']);

    sendComment( 1, 1, "Topic Reply", $comment );
?>
<?php endif; ?>

是的,正在执行 javascript。序列化数据看起来不错(“id=value&anotherid=value”)

4

2 回答 2

0

要了解会发生什么,我建议: - 查看 Apache 日志(访问和错误)以查看您的 URL 是否被重写以及如何重写。- 从浏览器或 Chrome 插件 Postman 等工具手动运行查询(然后您可以看到 PHP 错误)。

这应该有助于发现问题!

于 2013-10-29T23:19:40.847 回答
0

我已经弄清楚了这个问题。我不得不使用 http.open("POST", '../comment.php'); javascript/html/css 不会在 php 等实际文件路径上拾取。(只是未来对其他人的帮助)

于 2013-10-29T23:27:37.927 回答