如何使用 JSON 编码的服务器响应填充对象属性?

发布于 2024-08-16 09:04:56 字数 670 浏览 5 评论 0原文

我怎样才能把这个:

<? 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

天赋异禀 2024-08-23 09:04:56

好的 - 您的问题似乎是对异步 API 工作方式的误解。 $.ajax() 发出请求,在将来的某个时刻 使用响应调用提供的回调。

假设您有要填充的对象的名称,则可以使用类似以下内容填写所需的属性:

var someObject = {
  ...
  rowData: null,
  ...
};

// at this point, someObject is incomplete...

$.getJSON("http://tarnfeldweb.com/applewebapps/ajax/recentApps.php", 
  function(data)
  {
    // called whenever the server gets around to sending back the data
    someObject.rowData = data;
    // at this point, someObject is complete.
  });

请注意,我在这里使用 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:

var someObject = {
  ...
  rowData: null,
  ...
};

// at this point, someObject is incomplete...

$.getJSON("http://tarnfeldweb.com/applewebapps/ajax/recentApps.php", 
  function(data)
  {
    // called whenever the server gets around to sending back the data
    someObject.rowData = data;
    // at this point, someObject is complete.
  });

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.

森罗 2024-08-23 09:04:56

试试这个:

var rowData;
$.getJSON("http://tarnfeldweb.com/applewebapps/ajax/recentApps.php", function(data) {
    rowData = data;
});

但请注意,在调用回调函数(请参阅 getJSON 调用的第二个参数)之前,rowData 不可用。

Try this:

var rowData;
$.getJSON("http://tarnfeldweb.com/applewebapps/ajax/recentApps.php", function(data) {
    rowData = data;
});

But note that rowData is not available until the callback function (see second parameter of getJSON call) has been called.

丑丑阿 2024-08-23 09:04:56

这个问题基本上已经使用另一种方法得到了回答,但是,如果您有兴趣使用 $.ajax 方法而不是 $.getJSON 方法,这就是方法你会这样做:

var rowData;
$.ajax({
    url: "http://tarnfeldweb.com/applewebapps/ajax/recentApps.php",
    type: 'get',
    dataType: 'json' // could also be 'jsonp' if the call is going to another site...
    success: function(data){
        rowData = data; 
    }
});

只是另一种选择,仅此而已......

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:

var rowData;
$.ajax({
    url: "http://tarnfeldweb.com/applewebapps/ajax/recentApps.php",
    type: 'get',
    dataType: 'json' // could also be 'jsonp' if the call is going to another site...
    success: function(data){
        rowData = data; 
    }
});

Just another option, that's all...

笑,眼淚并存 2024-08-23 09:04:56

看来您不明白如何 $.ajax有效(看起来没有人这样做)。

$.ajax 是一个异步函数,这意味着它不返回任何内容。它只准备一个稍后在收到数据后调用的函数。

你的代码应该看起来更像这样:

var obj = {
 ...
 _rowData: [],
 ...
};

$.getJSON("http://tarnfeldweb.com/applewebapps/ajax/recentApps.php", function(data)
{
    // Now data contains your row data, 

    // you can either fill the global object
    obj._rowData = data;
    doSomethingWith(obj);

    // or use data directly
    doSomethingWith({
        ...
        _rowData: data
        ...
    });  
});

// Note that code below will be executed before the AJAX data is received

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:

var obj = {
 ...
 _rowData: [],
 ...
};

$.getJSON("http://tarnfeldweb.com/applewebapps/ajax/recentApps.php", function(data)
{
    // Now data contains your row data, 

    // you can either fill the global object
    obj._rowData = data;
    doSomethingWith(obj);

    // or use data directly
    doSomethingWith({
        ...
        _rowData: data
        ...
    });  
});

// Note that code below will be executed before the AJAX data is received
霞映澄塘 2024-08-23 09:04:56

您的函数将返回成功响应的数据。
您已经声明了回调函数:

function(data){
    return(JSON.stringify(data));
}

因此,如果您将内容类型声明为 text/json (或 application/json - 我不记得了,则“data”将包含从请求返回的任何数据)我的头)和
在响应中将 JSON 渲染为文本应该没问题。

您可能想要做的是让您的函数将变量声明为 rowData 并从那里开始,因此请执行以下操作:

function(rowData){
    // do something with the rowdata
}

我怀疑您需要 stringify 方法,除非您尝试将数据作为文本写出。

Your function will return the data on a successful response.
You have declared the callback function:

function(data){
    return(JSON.stringify(data));
}

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:

function(rowData){
    // do something with the rowdata
}

I doubt you need the stringify method unless you're trying to write the data out as text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文