0

我在 javascript 中有一个函数,到目前为止我已经对其进行了硬编码:

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"}
    ];
}

现在..这只是对代码是否可以工作进行单元测试..但现在它正在工作..

我在后端生成这些数据并将其映射到服务器上的目录(目前是 localhost)

所以如果我去 localhost:5000/cardata 我有完全相同的数据,即

[
    {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"}
];

为了使这项工作,我应该对我的 javascript 文件进行哪些更改?

4

1 回答 1

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

不要忘记包含 jQuery 库

于 2013-02-12T22:52:27.983 回答