Skip to content

Cross-domain support (JSON/P) #7

@grimen

Description

@grimen

After some rumbling I got js-model to work with cross-domain scripting - which is vey much a requirement for most web apps today. Not complete, but here's how the AJAX call should look like:

return jQuery.ajax({
        type: method,
        url: url + '.json',
        dataType: "jsonp",
        jsonpCallback: 'JSONP',
        data: data,
        dataFilter: function(data, type) {
          return /\S/.test(data) ? data : null;
        },
        success: function(data) {
          self.xhrComplete({status: 200, responseText: data}, 'success', model, callback)
        },
        error: function(xhr, textStatus) {
          console.error(xhr)
        }
      });

As you can see I'm trying to fake a XHR-object here because XHR is not really used for JSONP-requests under the hood (using script tags hack), and "complete" callback don't appears to be triggered by jQuery for JSONP-requests. I'm hardcoding 200 status here which is a bit ugly I guess, but as I understand it JSONP-requests work a status code is not available - just error/success.

So personally I need this hack for my own and I bet plenty of js-model users will end up needing it as well. Is it of interest?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions