当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Writing AJAX to receive JSON file?

问题描述:

I'm trying to write an AJAX call using jQuery's $.ajax, but I'm not sure I have it correctly, especially the creating HTML part.

I'm really new at this, but I'm trying to write in the Javascript file, to create HTML that will list an avatar, name, location on the left hand column, and a date and story in the right hand column.

My JSON so far:

var user = [

{'avatar': 'image'

'author': 'name',

'location': 'location',

},

{'date': 'date',

'review': 'review'}

];

Attached is the Javascript AJAX file: http://jsfiddle.net/AryX8/

I'm pretty sure most of this is not written correctly, as I'm not sure exactly how to grab the JSON stuff and input it as HTML onto my HTML page. I have an empty div container on my HTML file so far. That's it. How do I incorporate these 3 documents together, especially how to write the ajax function call with the javascript for creating html in the success: area?

UPDATE:

I'm trying to have data in a JSON file. Retrieve that data by way of writing a Javascript/jQuery AJAX call, and have it output "through" my html file(in other words when this html file ie) index.html page is loaded, all the data from the JSON file will show up formatted via the Javascript/jQuery/AJAX success field.

网友答案:

Your JSON appears to be invalid, but the fiddle looks like it might work.

Try echoing something more like this from your server:

var validJSON = {
    "array": [
        {
            "avatar": "johnny",
            "location": "location"
        },
        {
            "avatar": "mikey",
            "location": "location"
        }
    ]
};
网友答案:

The JSON, should start with '{' so that like it compiles it all! The array you are providing starts with '['. Which means that this is an array for the variable or an object placed before it!

分享给朋友:
您可能感兴趣的文章:
随机阅读: