BloggerAds廣告

相關文章

2012年10月4日 星期四

load json crossdomain

Simply a note for loading json cross-domain

i use $.ajax() method for better event handling


client side js coding:
$.ajax({
 url:"http://another.domain.com",
 
 dataType: 'jsonp', //stand for "json-padding", it is jsonp, not json
 
 //its "jsonpCallback", not "jsonCallback". the callback method name from JSON, will explain below
 jsonpCallback: 'jsonCallback', 
 
 //set this to true to let the method know im loading data cross-domain
 crossDomain : true, 
 //success handler
 success: function(data) {
  console.log("dosomthing")
 },
 
 //error handler
 error: function() {
   console.log("error occured")
 }
});

the json response, wrap the JSON with a function with the name which declared in jsonpCallback above:
jsonCallback(
 {
  "user":"littleshell",
  "gender":"male"
 }
);