0

I have a function in javascript which I hard-coded as of now:

function getCarData() 
{
    return 
    [
        {car: "Nissan", year: 2009, chassis: "black", bumper: "black"},
        {car: "Nissan", year: 2006, chassis: "blue", bumper: "blue"},
        {car: "Chrysler", year: 2004, chassis: "yellow", bumper: "black"},
        {car: "Volvo", year: 2012, chassis: "white", bumper: "gray"}
    ];
}

Now.. this was just to unit test whether the code will work or not.. But now it is working..

I am generating this data on backend and am mapping it to the directory on server (which is localhost at the moment)

So if I go to localhost:5000/cardata I have exact same data i.e

[
    {car: "Nissan", year: 2009, chassis: "black", bumper: "black"},
    {car: "Nissan", year: 2006, chassis: "blue", bumper: "blue"},
    {car: "Chrysler", year: 2004, chassis: "yellow", bumper: "black"},
    {car: "Volvo", year: 2012, chassis: "white", bumper: "gray"}
];

What changes I should made on my javascript file in order to make this work?

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
frazman
  • 32,081
  • 75
  • 184
  • 269
  • You need to use an ajax call to retrieve the data from the URL. If you're not using a library and only plain javascript, then do a Google search for "javascript ajax". – jfriend00 Feb 12 '13 at 22:47
  • are you intending to consume this data in a Javascript function? if so, you might want to do an AJAX call to grab the Data. This might guide you `http://stackoverflow.com/questions/8951810/how-to-parse-json-data-with-jquery-javascript` – Akhil Feb 12 '13 at 22:49
  • @Akhil: Thats a bingo. :) Thanks – frazman Feb 12 '13 at 22:50

1 Answers1

1
$(document).ready(function()
{
    $.get('/cardata', function(data)
    {
        alert(data[0].car) //alerts "Nissan"
    });
})

don't forget to include the jQuery library

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Taron Mehrabyan
  • 2,199
  • 2
  • 20
  • 27
  • 2
    Nowhere in this question do it say anything about using jQuery. The convention is that if it isn't tagged with jQuery and jQuery isn't mentioned, then we should assume plain javascript. – jfriend00 Feb 12 '13 at 22:53
  • @TaronPro: Hi.. thanks for snippet.. but rather than alert.. can i return the data as desired.. because i have already wrote the rest of the code.. and the only change required is to get the data from /cardata? Thanks – frazman Feb 12 '13 at 22:54
  • @TaronPro: SO.. in function getCarData(){ cardata = some_way_to_get_data_using_your_block.. return cardata } ? – frazman Feb 12 '13 at 23:02
  • It is doing asynchronly, you can set cardData=data in callback function, its where I wrote alert() – Taron Mehrabyan Feb 12 '13 at 23:11
  • @TaronPro: Sorry to bothr you again. but i am if i do cardata = data and before returning do document.write(cardata) I am seeing undefined? – frazman Feb 12 '13 at 23:54
  • You mast write it after carddata=data in same function-callback. $.get('/cardata', function(data) { document.write(data) }); – Taron Mehrabyan Feb 13 '13 at 07:10