python解决跨域问题_ajax请求Python跨域问题的解决方式

1.如果用jquery请求不同服务的数据,或出现跨域 的问题,浏览器会阻止数据成功返回,下面是解决跨域的ajax请求:

varxhrurl ='http://127.0.0.1:5000/getfunc?content=今天天气很好,但是心情很糟糕,就是因为没有钱';

$.ajax({

type:"get",

async:false,

url:xhrurl,

cache:false,

dataType:"jsonp",

jsonp:"callbackparam",

jsonpCallback:"jsonpCallback1",

success:function(json){

alert(json);

},

error:function(e){

alert("error");

}

});

functionjsonpCallback1(data) {

alert(data);

}

Python代码需要做如下处理:

# -*- coding: utf-8 -*-

from snownlp import SnowNLP

import json

from flask import Flask, render_template, request, jsonify,Response

app = Flask(__name__)

# @app.route('/')

@app.route('/getfunc', methods=['POST', 'GET'])

def getfunc():

#data = json.loads(request.form.get('data'))

# resp = Response(data)

#Response.headers['Access-Control-Allow-Origin'] = '*'

# data = json.loads(request.get_json())

# story_data = json.loads(request.get_data().decode('utf-8'))

#context = data['lesson']

jsonp_callback = request.args.get('callback', 'jsonpCallback1') #这里的callback对应的值需要和ajax请求的callback保持一致。

context = request.values['content']

print(context)

s = SnowNLP(context)

'''

s = SnowNLP("是不是还不是就这样子了,完全不行,我觉得这车完全不行!")

'''

arry = []

'''' for sentence in s.sentences:

#print(sentence)

'''

'''

s0 = SnowNLP(s.sentences[0])

s1 = SnowNLP(s.sentences[1])

s2 = SnowNLP(s.sentences[2])

s3 = SnowNLP(s.sentences[3])

s4 = SnowNLP(s.sentences[4])

'''

_snownlpNum = 0

for k in s.sentences:

print(k)

_snownlpvalue = SnowNLP(k)

print(_snownlpvalue.sentiments)

arry.insert(_snownlpNum, _snownlpvalue.sentiments)

_snownlpNum + 1

'''

print(s0.sentiments)

print(s1.sentiments)

print(s2.sentiments)

print(s3.sentiments)

print(s4.sentiments)

arry.insert(0,s0.sentiments)

arry.insert(1,s1.sentiments)

arry.insert(2,s2.sentiments)

arry.insert(3,s3.sentiments)

arry.insert(4,s4.sentiments)

print(arry)

s2 = SnowNLP(sentence[1])

print("s2:"+s2.sentiments)

'''

positive = []

negative = []

value = 0

value1 = 0

num = 0

for i in arry:

# print(i)

if (i < 0.5):

print("负面词:" + str(i))

value += i

positive.insert(num, i)

num + 1

elif (i > 0.5):

print("正面词:" + str(i))

value1 += i

negative.insert(num, i)

num + 1

# ("负面词结果:" + str(value / 2))

# print("正面词结果:" + str(value1 / 3))

# print("正面词结果1:" + str((0.8342 + 0.8584 + 0.6251) / 3))

# print("负面词结果1:" + str((0.3280 + 0.3281) / 2))

print(negative)

print(positive.__len__())

print(positive)

# _result_positive = 0

# np.positive()

_result_positive = sum(positive)

_result_negative = sum(negative)

'''

print(_result_positive/positive.__len__())

print(_result_negative/negative.__len__())

print(_result_positive)

print(_result_negative)

'''

print(_result_positive / (_result_positive + _result_negative))

print(_result_negative / (_result_positive + _result_negative))

'''

_data_result1 = [{"_result_positive": _result_positive / (_result_positive + _result_negative),

"_result_negative": _result_negative / (_result_positive + _result_negative)},

{"_result_positive_len": positive.__len__(),

"_result_negative_len": negative.__len__()}]

_data_result = {"_result_positive":_result_positive/(_result_positive+_result_negative),"_result_negative":_result_negative/(_result_positive+_result_negative)}

'''

jsondate = {'_result_positive':_result_positive / (_result_positive + _result_negative),

'_result_negative':_result_negative / (_result_positive + _result_negative),

'_result_positive_len':positive.__len__(),

'_result_negative_len':negative.__len__()}

return Response( #return的时候需要通过response返回数据并且将callback一并返回给客户端,这样才能请求成功。

"%s(%s);" % (jsonp_callback, json.dumps({'ok': True, 'data': jsondate})),

mimetype="text/javascript"

)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值