No response when I tried to pass data to Flask endpoint from jquery
I have a web page where users enter data in a input field. I am trying to pass the entered data to a python script, process it and get back a response. I am running a XAMPP server and modified the httpd.conf file according to this answer.
Frontend:
<script>
let reviewtext;
$("#reviewbox").change(function () {
reviewtext = $('#reviewbox').val();
});
$("#submit").click(function (event) {
let message = {
review: reviewtext
}
$.ajax({
type: "POST",
url: "http://127.0.0.1:80/first.py",
data: JSON.stringify(message),
success: function (response) {
alert("Success");
},
error: function (data) {
alert("Fail");
},
});
});
</script>
Python:
#!C:/Users/xitis/AppData/Local/Programs/Python/Python37-32/python.exe
print("Content-Type: text/htmln")
from flask import request
from flask import jsonify
from flask import Flask
app = Flask(__name__)
@app.route('/first.py', methods=['POST'])
def predict():
response = {
'status' : "Success"
}
return jsonify(response)
When I run the web page, I dont get any response. The post request is not executing.
Is it even possible to do this ?
Do I have to recreate the entire web site using Flask for this to work ?
javascript python jquery ajax
add a comment |
I have a web page where users enter data in a input field. I am trying to pass the entered data to a python script, process it and get back a response. I am running a XAMPP server and modified the httpd.conf file according to this answer.
Frontend:
<script>
let reviewtext;
$("#reviewbox").change(function () {
reviewtext = $('#reviewbox').val();
});
$("#submit").click(function (event) {
let message = {
review: reviewtext
}
$.ajax({
type: "POST",
url: "http://127.0.0.1:80/first.py",
data: JSON.stringify(message),
success: function (response) {
alert("Success");
},
error: function (data) {
alert("Fail");
},
});
});
</script>
Python:
#!C:/Users/xitis/AppData/Local/Programs/Python/Python37-32/python.exe
print("Content-Type: text/htmln")
from flask import request
from flask import jsonify
from flask import Flask
app = Flask(__name__)
@app.route('/first.py', methods=['POST'])
def predict():
response = {
'status' : "Success"
}
return jsonify(response)
When I run the web page, I dont get any response. The post request is not executing.
Is it even possible to do this ?
Do I have to recreate the entire web site using Flask for this to work ?
javascript python jquery ajax
just access therequest.data
attribute.
– Claudio Santos
Nov 22 '18 at 11:52
Take a look here to avoid having your question flagged as irrelevant
– Claudio Santos
Nov 22 '18 at 11:58
add a comment |
I have a web page where users enter data in a input field. I am trying to pass the entered data to a python script, process it and get back a response. I am running a XAMPP server and modified the httpd.conf file according to this answer.
Frontend:
<script>
let reviewtext;
$("#reviewbox").change(function () {
reviewtext = $('#reviewbox').val();
});
$("#submit").click(function (event) {
let message = {
review: reviewtext
}
$.ajax({
type: "POST",
url: "http://127.0.0.1:80/first.py",
data: JSON.stringify(message),
success: function (response) {
alert("Success");
},
error: function (data) {
alert("Fail");
},
});
});
</script>
Python:
#!C:/Users/xitis/AppData/Local/Programs/Python/Python37-32/python.exe
print("Content-Type: text/htmln")
from flask import request
from flask import jsonify
from flask import Flask
app = Flask(__name__)
@app.route('/first.py', methods=['POST'])
def predict():
response = {
'status' : "Success"
}
return jsonify(response)
When I run the web page, I dont get any response. The post request is not executing.
Is it even possible to do this ?
Do I have to recreate the entire web site using Flask for this to work ?
javascript python jquery ajax
I have a web page where users enter data in a input field. I am trying to pass the entered data to a python script, process it and get back a response. I am running a XAMPP server and modified the httpd.conf file according to this answer.
Frontend:
<script>
let reviewtext;
$("#reviewbox").change(function () {
reviewtext = $('#reviewbox').val();
});
$("#submit").click(function (event) {
let message = {
review: reviewtext
}
$.ajax({
type: "POST",
url: "http://127.0.0.1:80/first.py",
data: JSON.stringify(message),
success: function (response) {
alert("Success");
},
error: function (data) {
alert("Fail");
},
});
});
</script>
Python:
#!C:/Users/xitis/AppData/Local/Programs/Python/Python37-32/python.exe
print("Content-Type: text/htmln")
from flask import request
from flask import jsonify
from flask import Flask
app = Flask(__name__)
@app.route('/first.py', methods=['POST'])
def predict():
response = {
'status' : "Success"
}
return jsonify(response)
When I run the web page, I dont get any response. The post request is not executing.
Is it even possible to do this ?
Do I have to recreate the entire web site using Flask for this to work ?
javascript python jquery ajax
javascript python jquery ajax
edited Nov 22 '18 at 12:42
Claudio Santos
1,1471020
1,1471020
asked Nov 22 '18 at 11:08
XitishXitish
35211
35211
just access therequest.data
attribute.
– Claudio Santos
Nov 22 '18 at 11:52
Take a look here to avoid having your question flagged as irrelevant
– Claudio Santos
Nov 22 '18 at 11:58
add a comment |
just access therequest.data
attribute.
– Claudio Santos
Nov 22 '18 at 11:52
Take a look here to avoid having your question flagged as irrelevant
– Claudio Santos
Nov 22 '18 at 11:58
just access the
request.data
attribute.– Claudio Santos
Nov 22 '18 at 11:52
just access the
request.data
attribute.– Claudio Santos
Nov 22 '18 at 11:52
Take a look here to avoid having your question flagged as irrelevant
– Claudio Santos
Nov 22 '18 at 11:58
Take a look here to avoid having your question flagged as irrelevant
– Claudio Santos
Nov 22 '18 at 11:58
add a comment |
1 Answer
1
active
oldest
votes
There are some things you should know before try this example.
First of all, the endpoint below is not valid, since you aren't exposing a python file to be acessible, but a HTTP endpoint.
url: "http://127.0.0.1:80/first.py",
You should expose a predict resources, and use the HTTP Verbs to change its state:
# Do prediction analysis
@app.route('/predict', methods=['POST'])
def predict():
result = store_predict_data(request.data)
response = {
'id': result.id,
'status' : result
}
return jsonify(response)
# get predict analysis result
@app.route('/predict/{id}', methods=['GET'])
def post_predictable_data():
...
As shown above, Flask uses werkzeug package, and at this package, there is a request object which has a data attribute, that allows you to get the payload you sent from javascript.
When you're using a web application framework, you'll end up having an app which communicates over HTTP, and as all network communication you should set the port, which is by default 5000 on Flask, you can change it to 80, but make sure you're the only one using it.
You can run your flask server, just for development purposes:
FLASK_APP=flask_webserver.py flask run --port 8080
* Serving Flask app "flask_webserver.py"
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
then, when you curl
it, you'll get a result:
curl -d 'forking' -X POST localhost:8080
Hello, forking World!
that's the code to reproduce the example above
from flask import Flask
from flask import request
app = Flask(__name__)
@app.route('/', methods=['POST','GET'])
def hello_world():
return 'Hello, %s World!' % request.data.decode('UTF-8')
UPDATE REGARDING XAMPP
Since you're running from XAMPP try to put the lines below at the end of your file.
if __name__ == '__main__':
app.run()
Take a look over here for details
I went through all the steps shown in the blog linked by you. that didn't fulfill my requirement. After the completion of the steps, I was able to run python scripts from apache server. How do I modify the steps so that I can open a HTML page from the server and pass and receive data to/from the python script?
– Xitish
Nov 25 '18 at 14:20
Are you just wanting to host a python web-server from Windows?
– Claudio Santos
Nov 26 '18 at 14:37
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429610%2fno-response-when-i-tried-to-pass-data-to-flask-endpoint-from-jquery%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are some things you should know before try this example.
First of all, the endpoint below is not valid, since you aren't exposing a python file to be acessible, but a HTTP endpoint.
url: "http://127.0.0.1:80/first.py",
You should expose a predict resources, and use the HTTP Verbs to change its state:
# Do prediction analysis
@app.route('/predict', methods=['POST'])
def predict():
result = store_predict_data(request.data)
response = {
'id': result.id,
'status' : result
}
return jsonify(response)
# get predict analysis result
@app.route('/predict/{id}', methods=['GET'])
def post_predictable_data():
...
As shown above, Flask uses werkzeug package, and at this package, there is a request object which has a data attribute, that allows you to get the payload you sent from javascript.
When you're using a web application framework, you'll end up having an app which communicates over HTTP, and as all network communication you should set the port, which is by default 5000 on Flask, you can change it to 80, but make sure you're the only one using it.
You can run your flask server, just for development purposes:
FLASK_APP=flask_webserver.py flask run --port 8080
* Serving Flask app "flask_webserver.py"
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
then, when you curl
it, you'll get a result:
curl -d 'forking' -X POST localhost:8080
Hello, forking World!
that's the code to reproduce the example above
from flask import Flask
from flask import request
app = Flask(__name__)
@app.route('/', methods=['POST','GET'])
def hello_world():
return 'Hello, %s World!' % request.data.decode('UTF-8')
UPDATE REGARDING XAMPP
Since you're running from XAMPP try to put the lines below at the end of your file.
if __name__ == '__main__':
app.run()
Take a look over here for details
I went through all the steps shown in the blog linked by you. that didn't fulfill my requirement. After the completion of the steps, I was able to run python scripts from apache server. How do I modify the steps so that I can open a HTML page from the server and pass and receive data to/from the python script?
– Xitish
Nov 25 '18 at 14:20
Are you just wanting to host a python web-server from Windows?
– Claudio Santos
Nov 26 '18 at 14:37
add a comment |
There are some things you should know before try this example.
First of all, the endpoint below is not valid, since you aren't exposing a python file to be acessible, but a HTTP endpoint.
url: "http://127.0.0.1:80/first.py",
You should expose a predict resources, and use the HTTP Verbs to change its state:
# Do prediction analysis
@app.route('/predict', methods=['POST'])
def predict():
result = store_predict_data(request.data)
response = {
'id': result.id,
'status' : result
}
return jsonify(response)
# get predict analysis result
@app.route('/predict/{id}', methods=['GET'])
def post_predictable_data():
...
As shown above, Flask uses werkzeug package, and at this package, there is a request object which has a data attribute, that allows you to get the payload you sent from javascript.
When you're using a web application framework, you'll end up having an app which communicates over HTTP, and as all network communication you should set the port, which is by default 5000 on Flask, you can change it to 80, but make sure you're the only one using it.
You can run your flask server, just for development purposes:
FLASK_APP=flask_webserver.py flask run --port 8080
* Serving Flask app "flask_webserver.py"
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
then, when you curl
it, you'll get a result:
curl -d 'forking' -X POST localhost:8080
Hello, forking World!
that's the code to reproduce the example above
from flask import Flask
from flask import request
app = Flask(__name__)
@app.route('/', methods=['POST','GET'])
def hello_world():
return 'Hello, %s World!' % request.data.decode('UTF-8')
UPDATE REGARDING XAMPP
Since you're running from XAMPP try to put the lines below at the end of your file.
if __name__ == '__main__':
app.run()
Take a look over here for details
I went through all the steps shown in the blog linked by you. that didn't fulfill my requirement. After the completion of the steps, I was able to run python scripts from apache server. How do I modify the steps so that I can open a HTML page from the server and pass and receive data to/from the python script?
– Xitish
Nov 25 '18 at 14:20
Are you just wanting to host a python web-server from Windows?
– Claudio Santos
Nov 26 '18 at 14:37
add a comment |
There are some things you should know before try this example.
First of all, the endpoint below is not valid, since you aren't exposing a python file to be acessible, but a HTTP endpoint.
url: "http://127.0.0.1:80/first.py",
You should expose a predict resources, and use the HTTP Verbs to change its state:
# Do prediction analysis
@app.route('/predict', methods=['POST'])
def predict():
result = store_predict_data(request.data)
response = {
'id': result.id,
'status' : result
}
return jsonify(response)
# get predict analysis result
@app.route('/predict/{id}', methods=['GET'])
def post_predictable_data():
...
As shown above, Flask uses werkzeug package, and at this package, there is a request object which has a data attribute, that allows you to get the payload you sent from javascript.
When you're using a web application framework, you'll end up having an app which communicates over HTTP, and as all network communication you should set the port, which is by default 5000 on Flask, you can change it to 80, but make sure you're the only one using it.
You can run your flask server, just for development purposes:
FLASK_APP=flask_webserver.py flask run --port 8080
* Serving Flask app "flask_webserver.py"
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
then, when you curl
it, you'll get a result:
curl -d 'forking' -X POST localhost:8080
Hello, forking World!
that's the code to reproduce the example above
from flask import Flask
from flask import request
app = Flask(__name__)
@app.route('/', methods=['POST','GET'])
def hello_world():
return 'Hello, %s World!' % request.data.decode('UTF-8')
UPDATE REGARDING XAMPP
Since you're running from XAMPP try to put the lines below at the end of your file.
if __name__ == '__main__':
app.run()
Take a look over here for details
There are some things you should know before try this example.
First of all, the endpoint below is not valid, since you aren't exposing a python file to be acessible, but a HTTP endpoint.
url: "http://127.0.0.1:80/first.py",
You should expose a predict resources, and use the HTTP Verbs to change its state:
# Do prediction analysis
@app.route('/predict', methods=['POST'])
def predict():
result = store_predict_data(request.data)
response = {
'id': result.id,
'status' : result
}
return jsonify(response)
# get predict analysis result
@app.route('/predict/{id}', methods=['GET'])
def post_predictable_data():
...
As shown above, Flask uses werkzeug package, and at this package, there is a request object which has a data attribute, that allows you to get the payload you sent from javascript.
When you're using a web application framework, you'll end up having an app which communicates over HTTP, and as all network communication you should set the port, which is by default 5000 on Flask, you can change it to 80, but make sure you're the only one using it.
You can run your flask server, just for development purposes:
FLASK_APP=flask_webserver.py flask run --port 8080
* Serving Flask app "flask_webserver.py"
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
then, when you curl
it, you'll get a result:
curl -d 'forking' -X POST localhost:8080
Hello, forking World!
that's the code to reproduce the example above
from flask import Flask
from flask import request
app = Flask(__name__)
@app.route('/', methods=['POST','GET'])
def hello_world():
return 'Hello, %s World!' % request.data.decode('UTF-8')
UPDATE REGARDING XAMPP
Since you're running from XAMPP try to put the lines below at the end of your file.
if __name__ == '__main__':
app.run()
Take a look over here for details
edited Nov 22 '18 at 13:25
answered Nov 22 '18 at 12:38
Claudio SantosClaudio Santos
1,1471020
1,1471020
I went through all the steps shown in the blog linked by you. that didn't fulfill my requirement. After the completion of the steps, I was able to run python scripts from apache server. How do I modify the steps so that I can open a HTML page from the server and pass and receive data to/from the python script?
– Xitish
Nov 25 '18 at 14:20
Are you just wanting to host a python web-server from Windows?
– Claudio Santos
Nov 26 '18 at 14:37
add a comment |
I went through all the steps shown in the blog linked by you. that didn't fulfill my requirement. After the completion of the steps, I was able to run python scripts from apache server. How do I modify the steps so that I can open a HTML page from the server and pass and receive data to/from the python script?
– Xitish
Nov 25 '18 at 14:20
Are you just wanting to host a python web-server from Windows?
– Claudio Santos
Nov 26 '18 at 14:37
I went through all the steps shown in the blog linked by you. that didn't fulfill my requirement. After the completion of the steps, I was able to run python scripts from apache server. How do I modify the steps so that I can open a HTML page from the server and pass and receive data to/from the python script?
– Xitish
Nov 25 '18 at 14:20
I went through all the steps shown in the blog linked by you. that didn't fulfill my requirement. After the completion of the steps, I was able to run python scripts from apache server. How do I modify the steps so that I can open a HTML page from the server and pass and receive data to/from the python script?
– Xitish
Nov 25 '18 at 14:20
Are you just wanting to host a python web-server from Windows?
– Claudio Santos
Nov 26 '18 at 14:37
Are you just wanting to host a python web-server from Windows?
– Claudio Santos
Nov 26 '18 at 14:37
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429610%2fno-response-when-i-tried-to-pass-data-to-flask-endpoint-from-jquery%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
just access the
request.data
attribute.– Claudio Santos
Nov 22 '18 at 11:52
Take a look here to avoid having your question flagged as irrelevant
– Claudio Santos
Nov 22 '18 at 11:58