gpt4 book ai didi

javascript - 同源政策有问题

转载 作者:行者123 更新时间:2023-11-30 00:30:11 25 4
gpt4 key购买 nike

我的 java 脚本发布请求出现此错误“请求的资源上不存在 'Access-Control-Allow-Origin' header 。因此不允许访问 Origin 'null'。”

JavaScript(客户端)后置代码

function doapost(exam){
$.ajax({
type: "POST",
url: "http://localhost:8080/pexam",
data: exam,
dataType: "json",
success:function(e){alert("worked")}, // The function to call on success
error: function(e){alert("never worked")},
})
}

Python(网络服务)邮政编码

class postexamhandeler(webapp2.RequestHandler):
def post(self):
owner = self.request.get('owner')
name = self.request.get('ename')
date = self.request.get('edate')
leangth = self.request.get('eleangth')
pencil = self.request.get('epencil')
pen = self.request.get('epen')
calculator = self.request.get('ecalculator')
ruller = self.request.get('eruller')
type = self.request.get('etype')
self.response.headers["Content-Type"] = "application/json"
self.response.headers['charset'] = "utf-8"
if len(name) > 0 and len(type) > 0:
self.response.out.write('{ "response": "OK"}')
else:
self.response.out.write('{ "response": "ERROR"}')
exm = makeexam(id= owner)
exm.ename = name
exm.edate = date
exm.eleangth = leangth
exm.epencil = pencil
exm.epen = pen
exm.ecalculator = calculator
exm.eruller = ruller
exm.etype = type
exm.put()
self.response.out.write('{ "response": "OK"}')

最佳答案

您需要允许来自服务器的跨源请求。在 PHP 中,这可以解决问题

header('Access-Control-Allow-Origin: *');

通过查看您的 python 代码,我想这应该有所帮助(.. 我想)

class postexamhandeler(webapp2.RequestHandler):
def post(self):
owner = self.request.get('owner')
name = self.request.get('ename')
date = self.request.get('edate')
leangth = self.request.get('eleangth')
pencil = self.request.get('epencil')
pen = self.request.get('epen')
calculator = self.request.get('ecalculator')
ruller = self.request.get('eruller')
type = self.request.get('etype')
self.response.headers["Content-Type"] = "application/json"
self.response.headers["Access-Control-Allow-Origin"] = "*"
self.response.headers['charset'] = "utf-8"
if len(name) > 0 and len(type) > 0:
self.response.out.write('{ "response": "OK"}')
else:
self.response.out.write('{ "response": "ERROR"}')
exm = makeexam(id= owner)
exm.ename = name
exm.edate = date
exm.eleangth = leangth
exm.epencil = pencil
exm.epen = pen
exm.ecalculator = calculator
exm.eruller = ruller
exm.etype = type
exm.put()
self.response.out.write('{ "response": "OK"}')

这应该添加 cors 响应 header

self.response.headers["Access-Control-Allow-Origin"] = "*"

关于javascript - 同源政策有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29882805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com