如何使用 JSON 编码的服务器响应填充对象属性?
我怎样才能把这个:
<? echo json_encode($myArrays); ?>
...变成这个:
_rowData: [
{ name: "Most Recent", view: "recentView" },
{ name: "Most Popular", view: "popularView" },
{ name: "Staff Picks", view: "staffView" }
],
我的脚本返回^,但我不知道如何将数据放入字符串,_rowData
? PS 我正在使用 Dashcode,尝试将项目动态加载到列表控制器中
到目前为止,我有这个:
var recentListControllerXHR = $.ajax("http://tarnfeldweb.com/applewebapps/ajax/recentApps.php", function(data){
return(JSON.stringify(data));
}, 'text');
rowData: recentListControllerXHR,
How can i turn this:
<? echo json_encode($myArrays); ?>
...into this:
_rowData: [
{ name: "Most Recent", view: "recentView" },
{ name: "Most Popular", view: "popularView" },
{ name: "Staff Picks", view: "staffView" }
],
My script returns that ^, but i dont know how to put the data into the string, _rowData
?
P.S. I am using Dashcode, trying to dynamically load items into a List Controller
So far, i have this:
var recentListControllerXHR = $.ajax("http://tarnfeldweb.com/applewebapps/ajax/recentApps.php", function(data){
return(JSON.stringify(data));
}, 'text');
rowData: recentListControllerXHR,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好的 - 您的问题似乎是对异步 API 工作方式的误解。
$.ajax()
发出请求,在将来的某个时刻 使用响应调用提供的回调。假设您有要填充的对象的名称,则可以使用类似以下内容填写所需的属性:
请注意,我在这里使用 jQuery 对 JSON 的内置支持。如果您愿意,您可以使用 json.org 库,但是
getJSON()
还是比较方便的。
Ok - your problem appears to be a misunderstanding of how asynchronous APIs work.
$.ajax()
makes a request, and at some point in the future calls the provided callback with the response.Assuming you have a name for the object you're populating, you can fill in the desired property using something like this:
Note that I'm using jQuery's built-in support for JSON here. You can use the json.org library instead if you wish, but
getJSON()
is rather convenient if you don't have any unusual needs when parsing the data.试试这个:
但请注意,在调用回调函数(请参阅
getJSON
调用的第二个参数)之前,rowData
不可用。Try this:
But note that
rowData
is not available until the callback function (see second parameter ofgetJSON
call) has been called.这个问题基本上已经使用另一种方法得到了回答,但是,如果您有兴趣使用
$.ajax
方法而不是$.getJSON
方法,这就是方法你会这样做:只是另一种选择,仅此而已......
The question's essentially already been answered using another method, but, if you're interested in using the
$.ajax
method as opposed to the$.getJSON
method, this is how you would do that:Just another option, that's all...
看来您不明白如何
$.ajax
有效(看起来没有人这样做)。$.ajax
是一个异步函数,这意味着它不返回任何内容。它只准备一个稍后在收到数据后调用的函数。你的代码应该看起来更像这样:
Looks like you don't understand how
$.ajax
works (looks like nobody do).$.ajax
is an asynchronous function, which means it does not return anything. It only prepares a function to be invoked later, when the data has been received.Your code should look more like that:
您的函数将返回成功响应的数据。
您已经声明了回调函数:
因此,如果您将内容类型声明为 text/json (或 application/json - 我不记得了,则“data”将包含从请求返回的任何数据)我的头)和
在响应中将 JSON 渲染为文本应该没问题。
您可能想要做的是让您的函数将变量声明为 rowData 并从那里开始,因此请执行以下操作:
我怀疑您需要 stringify 方法,除非您尝试将数据作为文本写出。
Your function will return the data on a successful response.
You have declared the callback function:
So, 'data' will contain any data that was returned from the request, if you're declaring your content type as text/json (or application/json - I don't remember off the top of my head) and
rendering your JSON as text in the response you should be good.
What you probably want to do is have your function declare the variable as rowData and go from there, so do something like:
I doubt you need the stringify method unless you're trying to write the data out as text.