Javascript DOM errors

前端 未结 5 1110
没有蜡笔的小新
没有蜡笔的小新 2021-01-25 13:45

this is my javascript code , I am trying to create a dynamic list in HTML with data I recieve from the server , The data is of type \"json\"

My Javascript snippet

5条回答
  •  情深已故
    2021-01-25 14:25

    Here is a working version of your code that builds a list. Compare with your version to see where you made the mistakes, in both mark up and code.

    The source of your second error is incorrect data (most likely null) being passed to the addBooks function, so you would have to fix that.

    Chrome and Firebug have excellent JavaScript debuggers with breakpoints, so use that to your advantage to identify the issues:

    http://jsfiddle.net/tgth2/

    EDIT:

    Your problem is gleamingly obvious, after your comments and updates to the question:

    1. Your first page is loading the JSON data from the service, but then does window.location.replace("Page2 Updated.html");, which sends the browser to the new page (notice that you're calling addBooks(data); immediately after. But that code is never executed because browser has already gone to another page
    2. Your second page has in it, which will cause the function to be called with a null data. This is the cause of your problem.

    SOLUTION:

    Number one suggestion would be to start using jQuery for everything else you're doing, and not just for the AJAX call.

    Secondly, you should have the ajax call and the results rendering in one page, as it does not make any sense to redirect the browser to another page. Because your javascript always works in the context of a single page. As soon as you do something like window.location.replace(..) you end up losing everything you've done in the current page.

    If you make these changes, you will see that your list loads just fine!

提交回复
热议问题