Hi people, do you want to make the world a better place? Greener, smarter and so on? So just help me with this problem and for sure this will be reality !!!
Stop joking, I ask you to help me because it is two days I have this problem and I can't find a solution and the entire project is unusable.
I have a web site on "localhost" using some API provided by cherrypy on localhost:8085 (I must use API to do this project). The "simplified version of api.py is this (also in attachements):
import random
import string
import cherrypy
import mysql.connector
import json
class FindMeAPI(object):
exposed = True
@cherrypy.tools.accept(media='text/plain')
def GET (self, *uri, **params):
return "stupid string 1"
def POST (self, *uri, **params):
return "stupid string 2"
def DELETE (self, *uri, **params):
return "stupid string 3"
def CORS():
cherrypy.response.headers["Allow"] = "POST, GET, DELETE, OPTIONS"
cherrypy.response.headers["Access-Control-Request-Headers"] = "x-requested-with"
cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"
cherrypy.response.headers["Access-Control-Allow-Headers"] = "Origin, X-Requested-With, Content-Type, Accept"
cherrypy.response.headers["Content-Type"] = "application/json"
print cherrypy.response.headers
if __name__ == '__main__':
conf = {
'/': {
'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
'tools.sessions.on': True,
'tools.response_headers.on': True,
'tools.CORS.on': True,
'tools.response_headers.headers': [('Content-Type', 'text/plain')],
}
}
cherrypy.server.socket_host = '0.0.0.0'
cherrypy.tools.CORS = cherrypy.Tool('before_handler', CORS)
cherrypy.config.update({'server.socket_port': 8085})
cherrypy.quickstart(FindMeAPI(), '/findme', conf)
I use postman to check and.... Yeee! It works!
But, then I use AJAX in my html page to call the API (now to make it easy I removed the content of API and I put just some strings as returned value) with this javascript code:
function deleteTravel(){
var xhttp_deleteTravel = new XMLHttpRequest();
xhttp_deleteTravel.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log("it works!");
}
}
xhttp_deleteTravel.open("DELETE", "
http://localhost:8085/findme", true);
xhttp_deleteTravel.send();
}
And what I get is this error message in console:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at
http://localhost:8085/findme?id=10. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).[Learn More]
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at
http://localhost:8085/findme?id=10. (Reason: CORS request did not succeed).[Learn More]
Any idea to help me?
It works with GET method but not with DELETE. Even if they basically do the same stuff. It seems that cherrypy is ignoring the line where I authorize the use of "DELETE" .
Please, help me. Maybe the world wouldn't be smarter, but for me this task is very important.