0

I have a jsonrpc web service which is deployed on my redhat linux AMI. I can access this service in python as given below:

>>> import jsonrpclib
>>> server=jsonrpclib.Server(redhat_linux_ami_jsonrpc)
>>> x=server.addmicroelement('test test test')
>>> x
u'first insert'

where , redhat_linux_ami_jsonrpc = jsonrpc service hosted on redhat linux AMI

But when I try to call it in jquery, it works in IE but fails in Firefox. The code I have written is given below:

    var req = {jsonrpc:'2.0', method: 'addmicroelement',id:(new Date).getTime()}; 
    req.params=['new new new'];
    $.support.cors = true;      
    $.ajax({
        crossDomain: true,
        url: redhat_linux_ami_jsonrpc,
        data: JSON.stringify(req),
        type: "POST",
        contentType: "application/json",
        success: function(rpcRes) {
          alert(rpcRes.result); 
        },
        error: function(err, status, thrown) {
          alert(status);
        }  
    });

where , redhat_linux_ami_jsonrpc = jsonrpc service hosted on redhat linux AMI. It says "Cross-Origin Request Blocked". How to resolve this?

1 Answers1

0

The "problem" is that cross-domain requests like this are no longer allowed for security reasons.

A possible solution is using Cross-Origin Resource Sharing (CORS), though I don't yet have experience with this:
http://enable-cors.org/ (Obtained from https://stackoverflow.com/a/18509333/1245352)

Another possibility is to use {dataType: 'jsonp'}, which is JSON with padding. Unfortunately, this automatically sets your request to a 'GET' instead of a 'POST', which is what you're currently using. A possible workaround is to change your webservice to use the 'GET' method instead. There are also a bunch of hacks to post data to jsonp: Post data to JsonP

You can also try using curl wrapped by a php script to make the request, and return the results.

Community
  • 1
  • 1
Sevak Avakians
  • 883
  • 8
  • 13