简体   繁体   English

jQuery不发送POST数据

[英]jQuery not sending POST data

So I have a really simple form on a website that's entirely AJAX based for loading its pages. 所以我在一个完全基于AJAX的网站上有一个非常简单的表单来加载它的页面。 The only way for this form to work would be for it to do some AJAX magic as well, so I set about doing it. 这种形式工作的唯一方法是它也可以做一些AJAX魔术,所以我开始着手这样做。 I had the form tested so I knew it all worked. 我测试了表格,所以我知道这一切都奏效了。

Here's the javascript for my form. 这是我的表单的javascript。 The variable "fullpath" just tells me what page is loaded at the moment, all of the pages are stored in the local "pages" directory. 变量“fullpath”只是告诉我此刻加载了哪个页面,所有页面都存储在本地“pages”目录中。 It serializes the form and sends it to the server, with some debugging alerts. 它将表单序列化并将其发送到服务器,并提供一些调试警报。

$(document).ready(function() {
$("#regForm").submit(function(event) {
    alert($(this).serialize());
        $.post("pages/" + fullpath, $(this).serialize(), function(data){
            alert(data);                      
        });
        return false;
  });
});

Here's the form itself 这是形式本身

<form name="input" id="regForm">
<div class="form-field"><label>Username</label> <input type="text" name="username"/></div>
<div class="form-field"><label>Password</label> <input type="password" name="password"/></div>
<div class="form-field"><label>Confirm Password</label> <input type="password"  name="password2"/></div>
<div class="form-field"><label>Screen Name</label> <input type="text" name="screenname"/></div>
<div class="form-field"><label>Email Address</label> <input type="text" name="address"/></div>
<div class="form-field"><label>Group</label> <select name="usergroup"> 
<option value="0">Superuser</option>
<option value="1">Admin</option>
<option value="2">Moderator</option>
<option value="3">Advmember</option>
<option value="4">Member</option>
<option value="5">Guest</option>
</select> <br />
<label>Submit: </label><input type="submit" value="Submit" />
</div>
</form>

And here's some PHP I put at the beginning of the page 这是我在页面开头放的一些PHP

print_r($_POST);

So I fill the form with some bogus info, and I press submit. 所以我填写了一些虚假信息表格,然后按提交。 All of the data is displayed with the 所有数据都显示在

alert($(this).serialize());

And then the call is successful and I see the loaded form with my 然后调用成功,我看到加载的表单与我的

alert(data);

But, where I ask to print the $_POST array in PHP, this is all I get 但是,我要求在PHP中打印$ _POST数组,这就是我得到的

Array ()

So jQuery is sending the data, it's getting the page back, but for some reason the POST variables aren't going through. 所以jQuery正在发送数据,它正在恢复页面,但由于某种原因,POST变量没有通过。 Anyone care to lend a hand? 有人想帮忙吗?

This works in a Fiddle . 这适用于小提琴

Are you sure that fullpath is defined globally ? 您确定全局定义了fullpath吗? I don't see any other possible source of errors in your code. 我没有在您的代码中看到任何其他可能的错误来源。

Edit : I can see the actual problem from your comments: 301 redirects don't work through POST : 编辑 :我可以从您的评论中看到实际问题:301重定向无法通过POST工作

If the 301 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued. 如果收到301状态代码以响应GET或HEAD以外的请求,则用户代理不得自动重定向请求,除非用户可以确认,因为这可能会改变发出请求的条件。

You need remove this redirect thing, so "pages/" + fullpath directly points to the PHP script. 你需要删除这个重定向的东西,所以"pages/" + fullpath直接指向PHP脚本。 This could also be a problem with your server configuration. 这也可能是您的服务器配置问题。

In case of Apache, you might also want to have a look at this SO question . 在Apache的情况下,您可能还想看看这个SO问题

I packed your snippets together in an html-file and it worked for me so the problem has to be somewhere else in your code. 我将你的代码段打包在一个html文件中,它对我有用,所以问题必须在你的代码中的其他地方。 (source: http://pastebin.com/y4Dfsepv ) (来源: http//pastebin.com/y4Dfsepv

you need to chance your direct link."pages/" + fullpath. 你需要机会直接链接。“pages /”+ fullpath。 That is a problem, ajax can't recognize your link when you post 这是一个问题,当你发帖时,ajax无法识别你的链接

You have not specified method="post" in your form . 您尚未在表单中指定method =“post” If you do not specify, it becomes method="get" by default. 如果未指定,则默认情况下变为method =“get”。 So there is no values in the $_POST, you can print_R($_GET) and you will see values there. 所以$ _POST中没有值,你可以print_R($ _ GET),你会在那里看到值。

Change the below line from: 更改以下行:

<form name="input" id="regForm">

to: 至:

 <form name="input" id="regForm" method="post">

Update: 更新:

Updating the answer as per the comment. 根据评论更新答案。 The "pages/" + fullpath in $.post might be pointing to the wrong page, try alerting it and check server response in firebug. $ .post中的“pages /”+ fullpath可能指向错误的页面,尝试提醒它并检查firebug中的服务器响应。 Make sure it is pointing to the page you want else use the full path to the php script like below: 确保它指向您想要的页面,否则使用php脚本的完整路径,如下所示:

$.post("http://localhost/pages/" + fullpath, $(this).serialize(), function(data)

** Editing because we've learned that there is a 301 Redirect code being returned by the server ** **编辑因为我们已经知道服务器返回了301重定向代码**

See this: https://mdk.fr/blog/post-data-lost-on-301-moved-permanently.html 请参阅: https//mdk.fr/blog/post-data-lost-on-301-moved-permanently.html

301 Redirects lose contents of the POST. 301重定向丢失POST的内容。 So jQuery is sending it along, but the server redirects it to the right location of the script without the POST data. 所以jQuery正在发送它,但是服务器将它重定向到脚本的正确位置而没有POST数据。 You need to figure out where the right location of the script is and specify that in your jquery call to avoid the redirect. 您需要确定脚本的正确位置,并在jquery调用中指定以避免重定向。

您的表单中没有method =“POST”。

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

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