Skip to content

Instantly share code, notes, and snippets.

@lkatney
Last active August 29, 2015 14:09
Show Gist options
  • Save lkatney/d7bdd012650a83afee73 to your computer and use it in GitHub Desktop.
Save lkatney/d7bdd012650a83afee73 to your computer and use it in GitHub Desktop.
Apex rest service to send JSONP response
@RestResource(urlMapping='/Accounts/*')
global class AccountsService {
@HttpGet
global static void getAccounts() {
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String callback = req.params.get('callback');
String result;
try{
List<Account> accs = [SELECT Id from Account LIMIT 0;]
}
if(!holidays.isEmpty()){
result = JSON.serialize(accs);
}
}catch(Exception e){
res.statusCode = 500;
result = '{"message" : "An error has encountered at server, Please try after some time!"}';
}
}
res.addHeader('Content-Type','application/json; charset=utf-8');
res.responseBody = Blob.valueOf(callback+'('+result+')');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment