1
我試圖使用JQuery執行跨域AJAX請求。服務器正在執行用Python編寫的CGI文件。我試着加入CORS支持到服務器,但它似乎沒有奏效,他說:XHR無法加載<URL>。請求的資源上沒有「Access-Control-Allow-Origin」標頭
XMLHttpRequest cannot load http://<URL>.cgi. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin <Other-URL> is therefore not allowed access.
jQuery的AJAX請求是:
$.ajax({
type: "POST",
url: "http://<URL>.cgi",
})
.done(function(msg) {
alert(msg);
});
和服務器腳本是:
import os
import cgi
import cgitb; cgitb.enable()
import sys
import mysql.connector
from mysql.connector import errorcode
form = cgi.FieldStorage()
Username = form.getvalue('username')
Email = form.getvalue('email')
Password = form.getvalue('password')
print ("Content-Type: text/html")
print ("Access-Control-Allow-Origin: *")
print("<\h2>Welcome, your credentials are as follows Username: %s Email: %s Password: %s </h2>" % (Username, Email, Password))
print ("""
<html>
<head>
</head>
<body>
""")
try:
cnx = mysql.connector.connect(user='xxxxxx', password='xxxxxxx', host='127.0.0.1', database='xxxxxxx')
cursor = cnx.cursor()
#cursor.execute("INSERT INTO `user_information` (`User_ID`, `Email`, `Username`, `Password`) VALUES (NULL, %s, %s, %s)", (Email, Username, Password))
cursor.execute("SELECT * FROM `user_information` LIMIT 0 , 30")
data = cursor.fetchall()
print(data)
cnx.commit()
cursor.close()
cnx.close()
except mysql.connector.Error as err:
print("Something went wrong: {}".format(err))
print ("""
</body>
</html>
""")
「我試過向服務器添加CORS支持」在哪裏?在你的代碼中我看不到任何這樣的內容(但很多不相關的MySQL代碼)。 –
print(「Access-Control-Allow-Origin:*」) – Jalfor
試試另外兩個頭文件:'Access-Control-Allow-Methods:GET,POST,PUT,OPTIONS'和'ccess-Control-Allow-Headers:Origin,接受,Content-Type,X-Requested-With,X-CSRF-Token'。 –