按下按钮时未显示JSON文件

时间:2019-07-03 08:07:23

标签: javascript json

我正在尝试使我的JSON文件在我按下按钮时显示在页面上,但是由于某些原因,代码无法获取该文件。

当我按下按钮时,它应该显示4个数组。

var autotila = document.getElementById("autolista")
var nappi = document.getElementById("nappi");

nappi.addEventListener("click", function() {
  var auto = new XMLHttpRequest();
  auto.open('GET', 'cars.json');
  auto.onload = function() {
    var ourData = JSON.parse(auto.responseText);
    lisaahtml(ourData);
  };
  auto.send();
});


function lisaahtml(data) {
  var htmlpatka = "";
  for (i = 0; i < data.lenght; i++) {
    htmlpatka += "<p>" + data[i].manufacturer + "," + data[i].model + "," + data[i].price + "," + data[i].wiki + "</p>";
  }
  autotila.insertAdjacentHTML('beforeend', htmlpatka)
}
<button id="nappi" value="nappi" type="button">Paina</button>
<div id="autolista"></div>

{
    "data" : [{ 
        "manufacturer" : "Porsche",
        "model" : "911",
        "price" : 135000,
        "quality" : [{ 
            "name" : "overall",
            "rating" : 3
        },{ 
            "name" : "mechanical",
            "rating" : 4
        },{ 
            "name" : "powertrain",
            "rating" : 2
        },{ 
            "name" : "body",
            "rating" : 4
        },{ 
            "name" : "interior",
            "rating" : 3
        },{ 
            "name" : "accessories",
            "rating" : 2
        }],
        "wiki" : "http://en.wikipedia.org/wiki/Porsche_997"
    }
}

2 个答案:

答案 0 :(得分:2)

看似有错字:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{

    WNDCLASSEX wcex = {};
    wcex.cbClsExtra = 0;
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.cbWndExtra = 0;
    wcex.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE (IDI_ICON));
    wcex.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE (IDI_ICON));
    wcex.hInstance = hInstance;
    wcex.lpfnWndProc = WndProc;
    wcex.lpszClassName = TEXT("WinApp");
    wcex.lpszMenuName = NULL;
    wcex.style = CS_HREDRAW | CS_VREDRAW;

    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL, TEXT("RegisterClassEx Failed!"), TEXT("Error"), MB_ICONERROR);
        return EXIT_FAILURE;
    }

    HWND hWnd = CreateWindow(
        TEXT("WinApp"), TEXT("SeqGen"),
        WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
        CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT,
        NULL, NULL, hInstance, NULL
    );

    if (!hWnd)
    {
        MessageBox(NULL, TEXT("CreateWindow Failed!"), TEXT("Error"), MB_ICONERROR);
        return EXIT_FAILURE;
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    hdc = GetDC(hWnd);

    // Messages
    MSG msg;

    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (IsDialogMessage(hWnd, &msg))
        {}
        else
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return EXIT_SUCCESS;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

    LPCSTR moduleValue;
    static int index {};
    static unsigned module {0};
    static bool draw {false};

    switch (msg)
    {
        case WM_CREATE:
            hWndStatic = CreateWindow(
                TEXT("Static"), TEXT("Tube rows"),
                WS_CHILD | WS_VISIBLE,
                30, 20, 100, 24,
                hWnd, NULL, NULL, NULL
            );

            hWndStatic = CreateWindow(
                TEXT("Static"), TEXT("Water passes"),
                WS_CHILD | WS_VISIBLE,
                130, 20, 100, 24,
                hWnd, NULL, NULL, NULL
            );

            hWndStatic = CreateWindow(
                TEXT("Static"), TEXT("Multiples"),
                WS_CHILD | WS_VISIBLE,
                230, 20, 100, 24,
                hWnd, NULL, NULL, NULL
            );

            hWndrr = CreateWindowEx(
                WS_EX_CLIENTEDGE, TEXT("Edit"), TEXT(""),
                WS_CHILD | WS_VISIBLE | WS_TABSTOP,
                30, 50, 50, 24,
                hWnd, NULL, NULL, NULL
            );

            hWndvv = CreateWindowEx(
                WS_EX_CLIENTEDGE, TEXT("Edit"), TEXT(""),
                WS_CHILD | WS_VISIBLE | WS_TABSTOP,
                130, 50, 50, 24,
                hWnd, NULL, NULL, NULL
            );

            hWndN = CreateWindowEx(
                WS_EX_CLIENTEDGE, TEXT("Edit"), TEXT("0"),
                WS_CHILD | WS_VISIBLE | WS_TABSTOP,
                230, 50, 50, 24,
                hWnd, NULL, NULL, NULL
            );

            hWndList = CreateWindow(
                TEXT("ListBox"), TEXT(""),
                WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_AUTOVSCROLL | WS_BORDER | LBS_NOTIFY,
                20, 150, TEXTBOX_WIDTH, TEXTBOX_HEIGHT,
                hWnd, (HMENU) LST_RESULT, NULL, NULL
            );


            hWndCreateall = CreateWindow(
                TEXT("Button"), TEXT("Create all DIR"),
                WS_CHILD | WS_VISIBLE,
                50, 90, 100, 24,
                hWnd, (HMENU) BTN_CREATEALL, NULL, NULL
            );   

            hWndButton = CreateWindow(
                TEXT("Button"), TEXT("Create DIR"),
                WS_CHILD | WS_VISIBLE | WS_TABSTOP,
                200, 90, 100, 24,
                hWnd, (HMENU) BTN_CREATE, NULL, NULL
            );   

            break;

        case WM_COMMAND:
            switch (HIWORD(wParam))
            {
                case LBN_SELCHANGE:
                    if (LOWORD(wParam) == LST_RESULT)
                    {
                        index = SendMessage(hWndList, LB_GETCURSEL, 0, 0);

                        if (index >= 0)
                        {
                            module = 0;
                            moduleValue = (to_string(module).c_str());
                            SetWindowText(hWndStaticModule, moduleValue);
                            draw = true;
                            RedrawWindow(hWnd, 0, 0, RDW_INVALIDATE);
                        }
                    }
                    break;

                case BN_CLICKED:
                    switch (LOWORD(wParam))
                    {
                        case BTN_CREATE:
                            --stuff for creating combination--

                            break;

                        case BTN_CREATEALL:
                            --stuff for creating all possible combination--

                            break;
                    }

                    break;

                default:
                    break;
            }
            break;

        case WM_PAINT:
            if (draw)
            {
                --function for drawing stuff on main window--

                draw = false;
            }
            UpdateWindow(hWnd);

            break;

        case WM_DESTROY:
            PostQuitMessage(EXIT_SUCCESS);

        default:
            return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    return FALSE;
}

应该是

for (i = 0; i < data.lenght; i++)

答案 1 :(得分:0)

传递data.length

call