I am having some functions in javascript that does the string manipulations and then my backend updates that data to database.
In short, I have a url for sending data to backend, then this data is send to javascript fucntion, js does manipulation and the using ajax request I send the manipulated string to backend, which updates my database.
I am using flask framework
Here is what I have written till now
#this url is where I send a GET request
@app.route('/api',methods=['GET'])
def text():
text = request.args.get('text','')
lang = request.args.get('lang','')
return render_template('test.html',text=text,lang=lang)
Now JS does the manipulation of the strings and sends a ajax GET request to following url
@app.route('/files/<text>',methods=['GET'])
def fi(text):
if request.method == 'GET':
textValue = text.encode("utf-8")
INSERT_DB = 'INSERT INTO text (text) VALUES (%s)'
db = connect_mysql()
cursor = db.cursor()
cursor.execute(INSERT_DB,[textValue])
db.commit()
cursor.close()
db.close()
return ''
Now I check if the data is save to database or not in following url
@app.route('/test',methods=['GET'])
def test():
if request.method == 'GET':
db_select_last = "SELECT text FROM text order by id DESC limit 1"
db = connect_mysql()
cursor = db.cursor()
cursor.execute(db_select_last)
data = cursor.fetchone()
cursor.close()
db.close()
return (data['text'])
But what I face problem is when I manually hit the url from browser it updates the data. but when I send a GET request from python it doesn't. Why is this so.
Here's how I send a GET request to that url
main_url is http://fyp-searchall.rhcloud.com/
>>> import requests
>>> url = 'http://fyp-searchall.rhcloud.com/api?text=some body&lang=marathi'
>>> r = requests.get(url)
But data does not update. Where I am doing wrong ?
I got to know that JS works only when you have browser, so what should I do now?
In requests you should pass query parameters as a dictionary, using the params
keyword:
import requests
url = 'http://fyp-searchall.rhcloud.com/api'
params = {'text': 'some body', 'lang': 'marathi'}
r = requests.get(url, params=params)
See the Quickstart here: http://docs.python-requests.org/en/master/user/quickstart/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.