0

I'm using $http to post some data to my data base. Here is the documentation of the database. I use it on my terminal and it works.

Here's the error message I got from Safari's console:

1)Failed to load resource: Request header field 0 is not allowed by Access-Control-Allow-Headers. (seems to be sensed by the database)

2)XMLHttpRequest cannot load https://beta-api.mongohq.com/mongo/MyDId/myDatabse/collections/myCollection/documents. Request header field 0 is not allowed by Access-Control-Allow-Headers.

Here's my code:

factory.sendUrlTag = function(data){
        d = '{"document" : {"url_URL":"53738eef9256a31f4fdf6bf8","tag_Tag":"537375fc9256a31f4fdf6bf3"} }'

        return $http({
        url: 'https://beta-api.mongohq.com/mongo/MyDId/myDatabse/collections/myCollection/documents',
        method: "POST",
        data: d,
        headers: [
             {'Access-Control-Allow-Origin':'*'},
                  {'Access-Control-Allow-Headers':'Origin, X-Requested-With, Content-Type, Accept'},
            {'Content-Type': 'application/json'},
                {'Authorization' : 'api-key MyKey'}
                 ]
    })
    }

    return factory;
};

I didn't have " {'Access-Control-Allow-Origin':'*'}, {'Access-Control-Allow-Headers':'Origin, X-Requested-With, Content-Type, Accept'}," before, but I did some research after I got the error and added these. But it's still not working.

I do $http.get() in my app to the same database and it works.

This thing is driving me nuts.... Please help! Thank you all! :)

fuiiii
  • 1,359
  • 3
  • 17
  • 33

2 Answers2

0

Access-Control-Allow-Origin and friends are response headers, not request headers. It wouldn't make sense if Bob was responsible for granting Bob permission to Alice's system.

The server (https://beta-api.mongohq.com/mongo/MyDId/myDatabse/collections/myCollection/documents) has to send them, not the client.

Since you are making a cross-origin POST request, the server also needs to be able to respond to a pre-flight OPTIONS request.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Why my POST will be a cross-origin POST request? I don't know what's a cross-origin POST request but is there anyway I could fix it? – fuiiii May 14 '14 at 20:37
  • 1
    Because you are posting to `https://beta-api.mongohq.com/mongo/MyDId/myDatabse/collections/myCollection/documents` from an HTML document which is not hosted on `https://beta-api.mongohq.com/` – Quentin May 14 '14 at 20:38
  • Is there anyway to get around this issue? I could post it from my terminal. :/ I'm also using web2py as backend and using it to post data to the database and that works, but web2py isn't using $http. Is it because I use $http so it's "from an HTML document"? Thank you! – fuiiii May 14 '14 at 20:42
  • http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy – Quentin May 14 '14 at 20:43
  • Web2Py appears to serve a JavaScript laden HTML document to a browser. The code you have in the question looks like JavaScript (which, combined with the error message) suggests it runs on the client, and not server side Python. – Quentin May 14 '14 at 20:45
  • yes, my app use both web2py and angular.js. data directly submit by users go through web2py, and it's working. I'm trying to use angular to submit some data, and this part not working yet. thank you! – fuiiii May 14 '14 at 20:47
  • Presumably, it sets appropriate CORS headers. or maybe `$http.get` falls over to JSONP and the database supports it. Look at the traffic in your Net tab of your developer tools. – Quentin May 14 '14 at 20:56
0

I found some way maybe able to get around the issue: Use this and here to get around the cross origin origin issue. And this to get around the localhost

It may work.

Another relative post.

Community
  • 1
  • 1
fuiiii
  • 1,359
  • 3
  • 17
  • 33