使用 jQuery .ajax() 提交表单给我一个 null?
这是我的 JavaScript。我试图让 jQuery 将我的登录表单数据发送到我的 PHP 文件。然后我告诉它从 php 文件中获取数据并显示它。但所有回来的都是“空”。如果我请求另一个值,例如bio,它可以显示它。但它无法显示从我的表单发送的数据。
<script type="text/javascript">
$(document).ready(function() {
$("img").click(function() {
var data = $('form#signin').serialize();
$.ajax({
url:'signin.php',
type:'POST',
data: data,
success:function(data) {
$("p.test").html(data);
$.getJSON("signin.php", function(data) {
localStorage.email = data[0];
});
},
error:function(data) {
}
});
});
});
</script>
这是我的 PHP
<?php
$email = $_POST["email"];
$profile = array($email, "Karl", "Clement", "Gangsta Love!", "bio bio bio bio bio bio bio bio bio bio bio bio bio bio bio bio bio bio", "Ottawa", "http://a3.twimg.com/profile_images/1459354642/IMG_1560_normal.jpg");
header('Content-Type:text/json');
echo json_encode($profile);
?>
非常感谢您的帮助!
Here is my javascript. I am trying to have jQuery send my sign in form data over to my PHP file. I then tell it to grab the data from the php file and display it. But all that is coming back is "null". If I request another value such as bio, it can display it. But it cant display data sent from my form.
<script type="text/javascript">
$(document).ready(function() {
$("img").click(function() {
var data = $('form#signin').serialize();
$.ajax({
url:'signin.php',
type:'POST',
data: data,
success:function(data) {
$("p.test").html(data);
$.getJSON("signin.php", function(data) {
localStorage.email = data[0];
});
},
error:function(data) {
}
});
});
});
</script>
Here is my PHP
<?php
$email = $_POST["email"];
$profile = array($email, "Karl", "Clement", "Gangsta Love!", "bio bio bio bio bio bio bio bio bio bio bio bio bio bio bio bio bio bio", "Ottawa", "http://a3.twimg.com/profile_images/1459354642/IMG_1560_normal.jpg");
header('Content-Type:text/json');
echo json_encode($profile);
?>
Thank you so much for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
getJSON 发送 GET 请求,但您尝试访问 POST 变量。(确切地说,您没有通过 $.getJSON 发送任何变量)
数据类型设置为“json”的 $.ajax() 请求应该足够了:
getJSON sends a GET-request, but you try to access a POST-variable.(to be exactly you don't send any variables via $.getJSON )
The $.ajax()-request with a datatype set to 'json' should be sufficient:
您是否进行过测试以确保 php 正在发送数据回来?由于您处于phonegap 中,因此您需要引用服务器上的php,还要确保您已将plist 中的外部主机列入白名单。
在Xcode中,在phoneGap.plist文件中,您将看到外部主机,在那里添加一个新条目,键并不重要,但将值设置为*,这将允许所有外部域。
然后在你的 php 中回显一些“hello world”并在你的 ajax 中 console.log 该消息。
如果有效,那么您知道您能够访问您的 php,因此下一步是将变量传递给 is,首先回显
如果有效,下一步就是发回整个请求 -
只需控制台记录数据结果在分配任何 localStorage 之前,如果数据结果按预期返回,则像当前一样使用索引分配变量,但就像 dr.mobile 所说的那样,您不需要额外的请求,您已经拥有数据。
have you tested to make sure you php is sending data back? Since you're in phonegap you need to reference the php on your server, also make sure you've white listed the external hosts in your plist.
In xcode, in your phoneGap.plist file you'll see external hosts, add a new entry there, the key doesn't matter, but the value set to *, this will allow all external domains.
Then in your php just echo something "hello world" and console.log that message in your ajax.
If that works then you know your able to access your php, so the next step is to pass your variables over to is, start by just echoing
if that works the next step is to send back the whole request -
Just console log the data result before assigning any localStorage, if the data result comes back as expected then assign vars using the index like you are currently, BUT do just like dr.mobile said, you don't need that extra request, you already have the data.