美文网首页
ajax请求Python跨域问题的解决方式

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

作者: Mr内涵 | 来源:发表于2017-12-21 12:14 被阅读0次

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"

)

相关文章

  • ajax跨域请求

    ajax跨域请求(jsonp) 利用JSONP解决AJAX跨域问题的原理与jQuery解决方案JSONP jQue...

  • 解决ajax跨域问题

    Jsonp解决ajax跨域问题 CORS解决ajax跨域问题

  • Ajax跨域完全理解

    1. AJAX跨域完全问题的原因 浏览器出于安全考虑,特定限制 请求是跨域的 请求方式是XHR请求 2. 解决思路...

  • 跨域

    ??JSONP只能解决GET请求跨域,不能解决POST请求跨域问题,XHR2可以解决GET,POST方式的请求跨域...

  • 跨域产生的原因及解决办法

    1、跨域产生的原因及解决办法 2、JONP 与 ajax请求的区别 1⃣️请求方式不同: ajax请求Type...

  • Ajax请求跨域问题

    遇到ajax请求跨域问题,解决方式,改dataType为jsonp json和jsonp返回数据格式json格式 ...

  • webpack+vue+axios 环境下的跨域问题

    问题 在项目中向后端进行 ajax 请求时,出现游览器阻止跨域请求的问题。 引入 axios,并解决跨域 axio...

  • ajax请求Python跨域问题的解决方式

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

  • 14-vue-cli脚手架配置代理

    1. 问题:如果解决前端的 ajax 请求跨域的问题 首先可以发送 ajax 请求的方法有哪些? xhr js自带...

  • 解决跨域问题

    概述 在浏览器端进行 Ajax 请求时会出现跨域问题,那么什么是跨域,如何解决跨域呢?先看浏览器端出现跨域问题的现...

网友评论

      本文标题:ajax请求Python跨域问题的解决方式

      本文链接:https://www.haomeiwen.com/subject/bnaowxtx.html