function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

How to use JSONP format with apex REST?



I need to implement a REST service that returns data in JSONP format (JSONP format required for cross-domain ajax calls). I'm calling this service by jQuery ajax. But it always raises error : Error jQuery17109439062654184185_1342893046903 was not called.

The reason is that REST API only supports JSON and XML. In this case JSONP always returned as a XML. I'm wondering is there any workaround for this?


Here is my apex code:


global class noqBookingService {

global class BookingStatus {
    global String ReferenceId { get; set; }
    global integer Threshold { get; set; }

    global static String getQueueStatus() {
        String referenceId = req.params.get('referenceId');
        String jsonp_callback = req.params.get('callback');
        BookingStatus status = new BookingStatus();
        status.ReferenceId = referenceId;
        status.Threshold = 8;
        return jsonp_callback+'(['+JSON.serialize(status)+'])';        

 Visualforce code:


$(document).ready(function() {
            dataType: 'jsonp',
            contentType: 'application/javascript',
            success: function(status){ alert('success');},
            error:function(xhr, ajaxOptions, thrownError){ alert('error:'+thrownError);}




I have also faced same problem, however was not able to resolve.


As per my knowledge it is bug in JQuery. Please read more at -


Please post here if you find any solution.

I am experiencing the same - wondering if you've had any resolution?

Scott KrauseScott Krause
You cannot get there from here.  Meaning that it cannot be done.  The REST API must return JSONP in order for the browser to execute the script.  XML or JSON will not work.  I've tried in JQuery and with hand coded JavaScript.  Sorry.
I know this is a really late answer but anyone looking for this solution please use this for any GETs with REST: It worked great for me (follow the guide closely)! If you want to do POSTs with REST, you will have to make some authentication with something like (using php or something else for a proxy) since it is now spring 15+