Friday 15 May 2015

How jquery knows what dataType to return -


I am using php to generate some JSN data wrapped in a html5 tag and I'm not sure How jquery knows what type of data returns

This php

  echo & lt; Section id = "stuff" & gt; '; Echo json_encode ($ jsonData); Echo & lt; / Section & gt; ';   

To get data inside the section tag, I am treating my data like this

  $. Ajax ({url: 'getdata.php' data: "", data type: 'html', success: function (data) {var thedata = $ (.) Filter ('# content'). Html (); var Jobject = for JSON.parse (thedata); (Var i = 0; i & lt; jobject.length; i ++) {var item = jobject [i];   

here It is specified that the data will be html, but when I specify json, nothing

  $. Get ('getdata.php', function (data) {} . Var thedata = $ (data) .filter ('# stuff') .html (); var jobject = for JSON.parse (thedata); (var i = 0; i & lt; jobject.length; i ++ ) {Var item = Jquery short story method estimate my data publ HTML.   

Datatype indicates as a response type from jQuery In the first example, jQuery response is expected of the text (well, script tags evaluation with HTML), but when you specify JSON in Datatype jQuery, it will try to convert it to JavaScript object Which will fail.

In the later example, jQuery has anticipated JSON's response, ignoring filters and conversions in HTML, and was dealt with as JavaScript (Data) .

Overall, I'm encoding with the Jasonon Objects server within HTML, it should be structured and parsed as any other object.

  & lt; Section id = "stuff" & gt; '{"" Object ": [{" value ":" 1 "," text ":" HTML in 2 "}, {" value ":" 2 "," text ":" html in 3 "}}} ; / Section & gt; {{"Value": "1", "text": "2"}, {"value":   
  var json = '{object} "2", "text": "3"}]} 'var jobing = JSON Peers (jsn); (Var i = 0; i for lt; object for language.language; i ++) (var item = jobject.Object [i]; warning (item.Value);} var jsonInHtml = $ ("# Stuff "). Html (); Var job = JSON.parse (jsonInHtml); // A javascript object is not frmed for this point (var i = 0; i & lt; jobject.Object.length; i ++) Will be {var item = jobject.Object [i]; Warning (item.Value);}   



No comments:

Post a Comment