ajax請求Python跨域問題的解決方式

1.如果用jquery請求不同服務(wù)的數(shù)據(jù),或出現(xiàn)跨域 的問題,瀏覽器會阻止數(shù)據(jù)成功返回,下面是解決跨域的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對應(yīng)的值需要和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

# ("負面詞結(jié)果:" + str(value / 2))

# print("正面詞結(jié)果:" + str(value1 / 3))

# print("正面詞結(jié)果1:" + str((0.8342 + 0.8584 + 0.6251) / 3))

# print("負面詞結(jié)果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返回數(shù)據(jù)并且將callback一并返回給客戶端,這樣才能請求成功。

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

mimetype="text/javascript"

)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容