Jquery POST Json 返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5359666/
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
Jquery POST Json return
提问by njaknjak
i have a jquery script, which posts my form. here it is:
我有一个 jquery 脚本,它发布我的表单。这里是:
$(document).ready(function(){
$("form#submit").submit(function() {
var an = $('#an').attr('value');
var betreff = $('#betreff').attr('value');
var text = $('#text').attr('value');
$.ajax({
type: "POST",
url: "newmsg.php",
data: "an="+ an +"& betreff="+ betreff +"&text="+ text,
success: function(){
$('#window').html(name);
}
});
return false;
});
});
my newmsg.php file
我的 newmsg.php 文件
<?php if($_POST['an']=="john") { echo json_encode(array("name"=>"hi john")); } ?>
my problem is, that my php file will not return the name, so my div #window does not post the message
我的问题是,我的 php 文件不会返回名称,所以我的 div #window 没有发布消息
hope you guys understand...
希望大家理解...
thank you very much
非常感谢您
回答by mattsven
Try
尝试
success: function(data){
var json = $.parseJSON(data);
$('#window').html(json.name);
}
回答by Traveling_Monk
this is prob the part you have wrong
这可能是你错的部分
$('#an').attr('value');
if id=an is a input it should be done this way
如果 id=an 是输入,则应以这种方式完成
$('#an').val();
if id=an is a container it should be done this way
如果 id=an 是一个容器,则应该这样做
$('#an').html();
you'll want to change the rest of these too
你也想改变其余的
var betreff = $('#betreff').attr('value');
var text = $('#text').attr('value');