开发者

Iterate through MooTools JSONP output in JavaScript

开发者 https://www.devze.com 2023-02-02 03:44 出处:网络
I\'ve been experimenting with a .NET WCF Request/Response framework called Agatha. One of the example they have are using MooTools to call a JSONP extension and then show the results within a page in

I've been experimenting with a .NET WCF Request/Response framework called Agatha. One of the example they have are using MooTools to call a JSONP extension and then show the results within a page in a ASP.NET MVC app.

I've adapted an example which return's a single string to call a service which i have created which returns a collection of customers (from the AdventureWorks db).

I have very little experience with JavaScript in general and i'm stuck creating a function which will take the output from the service :

Request.JSONP.request_map.request_0({"ProcessJsonRequestsResult":[{"__type":"GetCustomerDetailsResponse:#Reference.ServiceLayer.Common.AdventureWorks.RequestAndResponse","Exception":null,"ExceptionType":0,"IsCached":false,"Customers":[{"CompanyName":"Trailblazing Sports","CustomerId":187,"FirstName":"Frank","LastName":"Campbell","MiddleName":null},{"CompanyName":"Authorized Bike Sales and Rental","CustomerId":199,"FirstName":"Roger","LastName":"Lengel","MiddleNam开发者_如何学Pythone":null},{"CompanyName":"Westside Plaza","CustomerId":599,"FirstName":"Raul","LastName":"Casts","MiddleName":"E."},{"CompanyName":"Westside Plaza","CustomerId":29641,"FirstName":"Raul","LastName":"Casts","MiddleName":"E."},{"CompanyName":"Trailblazing Sports","CustomerId":29938,"FirstName":"Frank","LastName":"Campbell","MiddleName":null},{"CompanyName":"Authorized Bike Sales and Rental","CustomerId":29942,"FirstName":"Roger","LastName":"Lengel","MiddleName":null}]}]});

And allow me to iterate through it so i can output the results into the page. Could anyone point me in the right direction ?


@pleasedontbelong: With JSONP you don't have to use JSON.decode

@John Kattenhorn: You should use the onComplete event, like the example in the docs: http://mootools.net/docs/more/Request/Request.JSONP

// ...

    onComplete: function(data){
        // You can, for example, iterate over the data
        // Assuming you have MooTools 1.3
        Object.each(data, function(value, key){

        });
    }

// ...

That's how you should use the data, the Request.JSONP.request_map.request_0 variable is private as well.


assuming that you are receiving the response of the request in Json format, you'll need to use Object.each to iterate the object

var response = '{"age": "25 years", "height": "170 cm", "weight": "120 kg", "name": "John", "lastName": "Doe"}';
    
var json = JSON.decode(response);
Object.each(json, function(item,index){
    alert("index:" + index + " item:" + item);
});

here i'm using a JSON.decode just to test... i believe that JSONP already gives you a json object

Hope this helps

0

精彩评论

暂无评论...
验证码 换一张
取 消