Created
November 7, 2014 04:09
-
-
Save ksomemo/1dd50cc1174294658af0 to your computer and use it in GitHub Desktop.
JSONP Sample Recommend
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var recommend_target = document.createElement('script'); | |
recommend_target.charset = 'utf-8'; | |
recommend_target.src = '/api/recommend?callback=_recommend_callback'; | |
document.body.appendChild(recommend_target); | |
console.log("recommend_target"); | |
})(); | |
function _recommend_callback(result) { | |
console.log("_recommend_callback"); | |
var recommend_list = document.createElement('ul'); | |
var i = 0, len = result.items.length; | |
for (; i < len; ++i) { | |
var item = result.items[i]; | |
var element = document.createElement('li'); | |
element.appendChild(document.createTextNode(item.item_name)); | |
recommend_list.appendChild(element); | |
} | |
document.body.appendChild(recommend_list); | |
console.log(result); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// using laravel | |
class RecommendController extends BaseController | |
{ | |
public function getIndex() | |
{ | |
$res = []; | |
$items = [ | |
['item_id' => '1', 'item_name' => 'name1'], | |
['item_id' => '2', 'item_name' => 'name2'], | |
['item_id' => '3', 'item_name' => 'name3'], | |
]; | |
$res['items'] = $items; | |
$res['status'] = 200; | |
return Response::json($res, 200)->setCallback(Input::get('callback')); | |
// => /**/callback({json: values}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment