Daniel Ruiz
Daniel Ruiz

Reputation: 351

Send json with POST

After seek in stackoverflow i can not saw a solution. I have this jquery code:

<script>

$('input#submit').click( function() {
    $.ajax({
        url: 'http://localhost:8081/rpc/api',
        type: 'post',
        contentType: 'application/json',            
        dataType: 'json',
        data: JSON.stringify({"jsonrpc": 2.0, "method": "entidades","params": {"file": "entidades.json"}}),
        success: function(data) {
                   alert(data);
                 },
        error:function(e){
            console.log(e);
        }
    });
});

I'm trying to send the json in data parameter to a file with a bottle server which processes the json and return something.

The problem is when i execute this from chrome or another browser, i receive this in the console:

    OPTIONS http://localhost:8081/rpc/api 405 (Method Not Allowed)
XMLHttpRequest cannot load http://localhost:8081/rpc/api. Origin http://localhost is not allowed by Access-Control-Allow-Origin. 

I think the problem is because i'm executing jquery in local. If this is the problem, how can do the same?

Upvotes: 0

Views: 81

Answers (2)

Dario Carabajal
Dario Carabajal

Reputation: 1

Access-Control-Allow-Origin means that you can't execute cross domain calls. https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

The only way you can solve this is allowing CORS in server. I think there is another workaround, using JSONP instead of using $.ajax(), but I've never used it

Upvotes: 0

TGH
TGH

Reputation: 39248

This looks like a cross domain request which is not allowed by most browsers by default. Even just different port numbers will make it a cross domain request. You can look into CORS as a possible solution.

http://www.html5rocks.com/en/tutorials/cors/

Another solution is to make a reverse proxy

Upvotes: 3

Related Questions