jQuery 如何在jquery中将ajax检索到的对象转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13927039/
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
How to convert ajax retrieved object to string in jquery
提问by ashish
I'm looking to make an object containing latitudes and longitudes of various places such as [["Second Event",19.0554748,72.8497017],["Demo Event",19.2097381,72.8737017]]
.
我希望制作一个包含各个地方的纬度和经度的对象,例如[["Second Event",19.0554748,72.8497017],["Demo Event",19.2097381,72.8737017]]
.
I'm successful of making this in php
by using json_encode()
function. How do I retrieve it in the callback function. I've tried the following:
我php
通过使用json_encode()
函数成功地做到了这一点。如何在回调函数中检索它。我尝试了以下方法:
$.post('maps1.php',{},function(data){
alert(data);
markers=JSON.stringify(data);
},"json");
alert(markers);
However this doesn't seems to work. What should I do?
然而,这似乎不起作用。我该怎么办?
回答by PhearOfRayne
You have the scope for the variable markers inside the post method, try doing it like this:
您在 post 方法中有变量标记的范围,请尝试这样做:
var markers = '';
$.post('maps1.php', {}, function (data) {
alert(data);
markers = JSON.stringify(data);
}, "json");
alert(markers)
回答by Jai
$.ajax
should do this way:
$.ajax
应该这样做:
var marker;
$.ajax({
url:'maps1.php',
data:{},
type:'POST',
dataType:'json',
success:function(data){
alert(data);
marker = JSON.stringify(data);
},
complete:{
alert(marker);
}
});
and $.POST
should be like this:
而$.POST
应该是这样的:
$.post('maps1.php',{},function(data){
alert(data);
},"json").done(function(data){
markers=JSON.stringify(data);
alert(markers);
});
but i prefer to use $.ajax()
但我更喜欢使用 $.ajax()
回答by gokul
ajax string syntax below:
ajax 字符串语法如下:
var marker;
$.ajax({
url:'maps1.php',
data:{},
type:'POST',
dataType:'json',
success:function(data){
alert(data);
marker = JSON.stringify(data);
},
complete:{
alert(marker);
}
});