0

I have this python script, working correctly and returning a valid response:

import requests
import json

url = 'http://loremipsum.com'
data = '{"text": 'google'}'
r = requests.post(url,data=json.dumps(data))
response = r.text
print(response)

The issuse is with the JS code, it keep throwing this error:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://loremipsum.com. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

$.ajax({
    type: 'POST',
    url: 'http://loremipsum.com',
    data: '{"text": 'google'}',
    crossOrigin: true,
    success: function(data) { alert('result: ' + data); },
    contentType: "application/json",
    dataType: 'json'
});

I tried dataType: jsonp Method not allowed by the server.

j44mvh
  • 21
  • 4
  • Change your code to instead use `url: 'https://cors-anywhere.herokuapp.com/http://loremipsum.com'`, and for an explanation, see https://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource/42744707#42744707 – sideshowbarker Aug 25 '17 at 04:10
  • if a site doesn't want to share resources with you, then there's little you can do to overcome that - that's what CORS is for, to stop people borrowing resources – Jaromanda X Aug 25 '17 at 04:10
  • `https://cors-anywhere.herokuapp.com/http://loremipsum.com` - file not found ... so, not good advice – Jaromanda X Aug 25 '17 at 04:11
  • `http://loremipsum.com` returns html, so you can't get JSON from it either – Jaromanda X Aug 25 '17 at 04:12
  • loremipsum.com is a placeholder. – Blacksun Aug 25 '17 at 05:13

0 Answers0