表单 .post 与 jquery 响应返回同一页面
尝试使用 jquery 发布表单,响应数据是当前页面的 html,而不是函数中指定的 url。
<form name="vote_form" class="vote_form" action="vote.php">
<input type="hidden" name="sid" id="sid" value="2056" />
<input type="submit" name="up" id="up" value="Vote Up" />
<input type="submit" name="down" id="down" value="Vote Down" />
</form>
这是 jquery
$(document).ready(function() {
$(".vote_form").submit(function() { return false; });
$("#up, #down").click(function(event) {
$form = $(this).parent("form");
$.post($form.attr("action"), $form.serialize() + "&submit="+ $(this).attr("name"), function(data) {
$('#results').html(data);
});
});
});
如果这里有问题,请参考 .htaccess
Options +ExecCGI
AddHandler php-cgi .php .html .htm .xml
Action php-cgi /cgi-bin/php.cgi
<FilesMatch "^php5?\.(ini|cgi)$">
Order Deny,Allow
Deny from All
Allow from env=REDIRECT_STATUS
</FilesMatch>
#Options +Indexes
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
我在这里读到一个问题,其中修复了将 URL 协议从 AUTO 更改为 URI_REQUEST 的类似问题,但我没有使用 CodeIgnitor 或任何其他框架。
我基本上已经从页面中删除了所有内容,只使用上面的表单和 jquery(vote.php 只打印出 $_POST 变量 - 也尝试了 $_REQUEST)。
基本上我很困惑。有什么想法吗?
Trying to post a form with jquery and the response data is the html from the current page, not the url specified in the function.
<form name="vote_form" class="vote_form" action="vote.php">
<input type="hidden" name="sid" id="sid" value="2056" />
<input type="submit" name="up" id="up" value="Vote Up" />
<input type="submit" name="down" id="down" value="Vote Down" />
</form>
Here's the jquery
$(document).ready(function() {
$(".vote_form").submit(function() { return false; });
$("#up, #down").click(function(event) {
$form = $(this).parent("form");
$.post($form.attr("action"), $form.serialize() + "&submit="+ $(this).attr("name"), function(data) {
$('#results').html(data);
});
});
});
For reference if there is an issue here, the .htaccess
Options +ExecCGI
AddHandler php-cgi .php .html .htm .xml
Action php-cgi /cgi-bin/php.cgi
<FilesMatch "^php5?\.(ini|cgi)$">
Order Deny,Allow
Deny from All
Allow from env=REDIRECT_STATUS
</FilesMatch>
#Options +Indexes
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
I read one question here with a similar issue fixed changing URL PROTOCOL from AUTO to URI_REQUEST but I am not using CodeIgnitor or any other framework.
I've basically stripped everything from the page and just using the form and jquery above (vote.php only prints out the $_POST variables - tried with $_REQUEST also).
Basically I'm stumped. Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否曾经尝试过
alert( $form.attr("action") )
只是为了确保您得到了正确的结果?还要用浏览器访问 vote.php 吗?Have you ever tried
alert( $form.attr("action") )
just to be sure you are getting the right thing? And also going to vote.php with your browser?