目前我正在通过 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”)