json - How do I pass AJAX JSONP data into a function? -
i asked similar question already, discovered needed use ajax , jsonp, opened whole new world of hurt.
a company working has api privacy policy , terms of use. need create 2 different pages:
- privacy policy
- terms of use
so need search data them, find correct term in "tags" ('ext_privacy' , 'ext_member_terms', respectively), , use html code array rest of page.
the problem is, can't figure out how pass data ajax function, , @ point, don't know how drill down sections appropriately - thought external.data.results.tags, doesn't seem work.
here's site code:
<!doctype html> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <title>privacy policy</title> </head> <body> <div id="data"> </div> <script> $.ajax({ type: 'get', url: 'http://guywhogeeks.com/test/test.json', jsonpcallback: 'jsoncallback', contenttype: 'application/json', cache: 'true', datatype: 'jsonp', success: datafeed }); function datafeed(external) { //establishing htmlstring - testing see if function fires var htmlstring = "hello, world!"; //i know way off - wanted add html htmlstring /*$(external.data.results.tags).find(privacy).each(function() { $({ text : external.data.results.html }).appendto(htmlstring) });*/ $('#data').html(htmlstring); } </script> </body> </html>
here's (slightly modified) json - hosted here:
{"code":200,"status":"ok","data": {"offset":0,"limit":20,"total":2,"target":"html_page","results":[ { "id":"6", "title":"privacy policy", "description":"privacy policy", "html":"<div class=\"right-rail\">\r\n<div class=\"module\">\r\n<h2 class=\"episode-guide-header\">web site privacy policy<\/h2>\r\n<div class=\"module-body corp-home-top\">\r\n<div class=\"corp-home-wrapper privacy\">\r\n<\/div>\r\n<!--corp-home-wrapper--><\/div>\r\n<!--module-body corp-home-top-->\r\n<div class=\"module-shadow\"> <\/div>\r\n<\/div>\r\n<!--module--><\/div>\r\n<!--right-rail-->", "tags":[ "ext_privacy" ] }, { "id":"66", "title":"license , tou", "description":"license agreement , terms of use", "html":"<!doctype html>\r\n<html>\r\n <head>\r\n <title>license agreement , terms of use<\/title>\r\n <\/head>\r\n <body>\r\n <div class=\"right-rail\">\r\n<div class=\"module\">\r\n<h2 class=\"episode-guide-header\"> license agreement , terms of use<\/h2>\r\n<\/div><\/body>\r\n<\/html>", "tags":[ "ext_member_terms" ] } ]} }
i'd appreciate can give me.
you jsonp payload incorrect. server should return call "callback" function... in example, server should return:
jsoncallback( {"code":200,"status":"ok","data": {"offset":0,"limit":20,"total":2,"target":"html_page","results":[ { "id":"6", "title":"privacy policy", "description":"privacy policy", "html":"<div class=\"right-rail\">\r\n<div class=\"module\">\r\n<h2 class=\"episode-guide-header\">web site privacy policy<\/h2>\r\n<div class=\"module-body corp-home-top\">\r\n<div class=\"corp-home-wrapper privacy\">\r\n<\/div>\r\n<!--corp-home-wrapper--><\/div>\r\n<!--module-body corp-home-top-->\r\n<div class=\"module-shadow\"> <\/div>\r\n<\/div>\r\n<!--module--><\/div>\r\n<!--right-rail-->", "tags":[ "ext_privacy" ] }, { "id":"66", "title":"license , tou", "description":"license agreement , terms of use", "html":"<!doctype html>\r\n<html>\r\n <head>\r\n <title>license agreement , terms of use<\/title>\r\n <\/head>\r\n <body>\r\n <div class=\"right-rail\">\r\n<div class=\"module\">\r\n<h2 class=\"episode-guide-header\"> license agreement , terms of use<\/h2>\r\n<\/div><\/body>\r\n<\/html>", "tags":[ "ext_member_terms" ] } ]} });
in client, should define "jsoncallback" like:
jsoncallback(data) { // data regular javascript object @ point // can access 'data.data.results[0].html' without problem }
check more information: http://en.wikipedia.org/wiki/jsonp
hope helps.
Comments
Post a Comment