ajax - Trouble parsing JSON with jQuery -


I'm playing with parsing JSON in jQuery and I'm having trouble. I want to check the value of the 'time' in the JSON object. Here's my test job:

  $ Ajax ({url: 'do_chat.php5', type: 'post', data: ({'message': '', 'poster': poster, 'log in_in': log in_in}, datatype: 'jason', success : Function (data) {$ .EEC (data, function (interval, message) {if (message ['time']) {$ ('# ChatWindow'). App ('& lt; p & gt; hi & lt ; / P & gt; ');}});}});  

looks like a string returning from my server:

{"poster" : "Message": "No Message!", "Time": 1256084677}

I'm sure that $. I use PHP to encode JSN string I am starting the life as a php assoc array. I am sure that my mistakes are very much and any help is appreciated!

Edit: Follow-up Question - How do I create PHP as a JSON array? I currently use it to create a single object:

  $ messages = mysqli_fetch_assoc ($ new_res); $ Msg = $ Message ["content"]; $ that = $ message ["poster"]; $ time = $ message ["time_ added"]; $ Message = array ('poster' = & gt; $, Which is the 'message' = & gt; $ Msg, 'time' = & gt; $ Time); Echo json_encode ($ message);  

But if I want to get more than one line from my database query, how do I do this?

{"poster": "", "message": "No message! "," Time "is returning: 1256084677}, but then you are going through it with $ .your .

$ Each expects an array, and since you are not returning an array, then $ .your is looping instead of your JSON object instead.

To fix your code, you have to make sure That your server is returning an array like: [{"poster": "", "message": "no message!", "Time": 1256084677}]

< Remove or

$ every so that you have:

  $. Ajax ({url : 'Do_chat.php5', type: 'post', data: ({'message': '', 'poster': poster, 'login in_in': log in_in}), datatype: 'jason', success: function (Data) {if (data [para ']) {$ (' # chat wando '). Append (' <'> hi & lt; / p & gt;';}}}); < / Code> 

Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -