Forums

get 500 (internal server error) jquery

Hi,

I am trying to use a Python script to connect to a remote database. I am calling this script from my phonegap application using $.getJSON(url), my script is stored on pythonanywhere, I want to use this script to send data to a database from my phonegap app, however no matter I do I keep getting the following error GET http://myurl 404 (Not Found) jquery.min.js:4 whenever I try and send data to the database

Can you be a bit more specific about the problem? What URL are you trying to fetch in the getJSON call? What happens if you visit that URL manually with your web browser? Does the URL point to a PythonAnywhere-hosted web app? If so, is there anything in its error log?

$.getJSON('http://pmcgrath1.pythonanywhere.com/insert' that is the url, its a script which I want to send data to a Heroku postgres database from my phonegap application. I want to use the script as a web service to my application. I have a script that I was using to test if this would work and that script creates a table in the heroku database, it works fine for me, but I cannot get a second script to send data to the Heroku database as I keep getting the 404 error. Its has the same connecti0n data as the script that works so I cant see why it keeps failing. I get the same error when I enter the url into my browser also The url for the script that works is here http://pmcgrath1.pythonanywhere.com/createTable, I have literlly copied the script and changed my database query but its failing all the time with that error

It sounds like the URL for the second script isn't configured correctly -- can you check that?

I have literally copied the script that works and changed the @app.route('') from @app.route('/createTable') to this @app.route('/insert') , that is the only difference between the two scripts

Did you hit "Reload" on the web tab?

Yeah I have done that, I have even tried a completely new script and uploaded it but I still have the same errors. I am new to pythonanywhere, is there any problem with using it for what I want it to do ? I want to send gps data from my phonegapp application to a Heroku database

May I take a look at your files?

Yeah absolutely, how will I send them to you ?

that's ok, as an admin I can see them directly on the site. Give me a sec...

OK I see what's going on. You've got your two URLs in two different files, and they are basically two totally different flask applications, but only one of them can be active at once.

If you want them in the same application, you need to have them in the same file, with a single app variable, and have all @app.route functions for different urls in the same place.

(there are more complicated ways to do it if you want multiple files, but the simplest thing is probably just to put everything in one file for now)

That's solved it, thanks a million for that.

I was having another problem inserting data from my app that wasnt hard coded, basically I am trying to use the data from a javascript function using a = request.args.get('data', 0, type=str) in my python insert script, I can print the contents of 'a' on my local machine so its definitely contains the data, but when I try to use 'a' in my insert query it never gets inserted into the database, and I have a warning on python anywhere saying the variable a is never used

here is my my python code: a = request.args.get('data', 0, type=str) conn = psycopg2.connect("connection string' ") cur = conn.cursor() cur.execute('''INSERT INTO work2 (ID)VALUES(a)''') conn.commit() conn.close()

Sorry that code has printed all on one line. its not very clear

you can use the little icons above the post window to format your post, or just do it manually, it's markdown syntax. there's little instructions in grey below the post window...

:python @app.route('/insert')

def index():

a = request.args.get('data', 0, type=str)
conn = psycopg2.connect("dbname='d7tuktpsceh55f' user='rlwwwdlcslgbmw' host='ec2-54-225-239-184.compute-1.amazonaws.com' password ='zVljCV2QtJddSUqG2Zjuh9w5zb' ")
cur = conn.cursor()
cur.execute('''INSERT INTO work2 (ID)VALUES(a)''')
conn.commit()
conn.close()

OK I think its more readable now.

And, I think your problem is that you need to pass the a variable as a parameter to your sql statement. Something like this:

cur.execute("INSERT INTO work2 (ID)VALUES(%s)", a)

I think running through a Flask tutorial might help?

Also, you might want to change your password now ;-)

oo, and there's a syntax error in that SQL statement I think. Needs an extra space or two:

cur.execute("INSERT INTO work2 (ID) VALUES (%s)", a)

Ok, thanks for your help