web.py ajax跨域传递json数据

明确一点跨域限制是浏览器行为,所以后端只需要设置一下header就行。
给出具体代码

import sys
import web
import simplejson as json
urls = (
    '/', 'index',
    '/login', 'login',
    '/start', 'start',
    '/load_trace', 'loadtrace',
    '/init', 'init'
)


class Conf:
    def __init__(self):
        web.header('content-type', 'text/json')
        web.header('Access-Control-Allow-Origin', '*')
        web.header('Access-Control-Allow-Methods', 'GET, POST')

    def get_json(self):
        data = web.data()
        data = json.loads(data)
        return data

class login:
    def POST(self):
    	self.get_json()
        return ''

header(‘Access-Control-Allow-Origin’, ‘*’)表示允许任意ip的请求。当然你也可以设置成特定ip。

猜你喜欢

转载自blog.csdn.net/qq_20949153/article/details/83242015
今日推荐