javascript 使用 $.ajax 发布不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23840128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
POSTing with $.ajax doesn't work
提问by Monopompom
Well, the problem is not new, as I saw (really surf a lot), but still can not find solution for myself.
嗯,这个问题并不新鲜,正如我所看到的(真的冲浪了很多),但仍然无法为自己找到解决方案。
Description of problem:
问题描述:
- Have local Web server - XAMPP;
- Firefox 29.0.1
And trying to send POST with $.ajax
function checkUser(){ $.ajax({ type: 'POST', url: 'ajax/checkUser.php', data: 'uname='+$("#username").val()+'&pword='+$("#password").val(), success: function(){ someNewFunc(); }, }); };
- 拥有本地 Web 服务器 - XAMPP;
- 火狐 29.0.1
并尝试使用 $.ajax 发送 POST
function checkUser(){ $.ajax({ type: 'POST', url: 'ajax/checkUser.php', data: 'uname='+$("#username").val()+'&pword='+$("#password").val(), success: function(){ someNewFunc(); }, }); };
This function I'm calling with:
我正在调用这个函数:
$(document).ready(function(){
$(".button.postfix.login").on("click", function(){
if (($("#username").val() != "") && ($("#password").val() != "")) {
hide(); <-- hide pop ups about empty field
checkUser();
} else {$("#empty").show();} <-- pop ups
});
});
And here code of page with elements:
这里是带有元素的页面代码:
<input id="username" type="text" placeholder="Username" />
<input id="password" type="password" placeholder="Password" />
<a class="button postfix login" href="#"
onclick="event.preventDefault();">Sign in</a>
I guess that data are going out, but the page checkUser.php doesn't get anything. What am I doing wrong?
我猜数据会出去,但页面 checkUser.php 没有得到任何东西。我究竟做错了什么?
P.S. checkUser.php:
PS checkUser.php:
<?php
print_r($_POST);
?>
Result - Array ( )
结果 - 数组 ( )
P.P.S. By the way, if POST change to GET it's working, but need to be POST
PPS 顺便说一句,如果 POST 更改为 GET 它可以工作,但需要 POST
采纳答案by Wesley Brian Lachenal
$(document).ready(function(){
$(".button.postfix.login").on("click", function() {
var username = $("#username").val();
var password = $("#password").val();
if ((username != "") && (password != "")) {
hide(); <-- hide pop ups about empty field
checkUser(username, password); // Pass the variables here as a parameter
} else {
$("#empty").show();} <-- pop ups
});
// Put the parameters here
function checkUser(username, password) {
$.ajax({
type: 'POST',
url: 'ajax/checkUser.php',
data: { username:username, password:password} //It would be best to put it like that, make it the same name so it wouldn't be confusing
success: function(data){
alert(data);
},
});
};
});
});
So when you're gonna call it on php
所以当你要在 php 上调用它时
$username = $_POST['username'];
echo $username;
回答by jordoncm
Does your someNewFunc
callback fire? Do you see the HTTP AJAX request in the developer console in Firefox?
你的someNewFunc
回调触发了吗?在 Firefox 的开发者控制台中看到 HTTP AJAX 请求了吗?
Try sending your post data as a dictionary to $.ajax
:
尝试将您的帖子数据作为字典发送到$.ajax
:
$.ajax({
type: 'POST',
data: {
uname: $("#username").val(),
pword: $("#password").val()
}
});