AJAX Post requests

P

PJ

Hi,
I have a simple web server using BaseHTTPServer, and the def do_POST
(self) function works fine for regular forms that are submitted to it,
but when I send an AJAX POST to it it does nothing (I've tried to just
get it to print to check it's nothing else but it doesn't even do
that, although it does for a regular POST request.
The post function is
def do_POST(self):
global rootnode
try:
ctype, pdict = cgi.parse_header(self.headers.getheader
('content-type'))
if ctype == 'multipart/form-data':
query=cgi.parse_multipart(self.rfile, pdict)
self.send_response(301)

self.end_headers()
print(query.get('data'))
print('test')
h = codecs.open ("log.txt", "a", encoding="utf8")
h.write("test")
h.close()


except :

pass

The AJAX post is appearing in the command prompt as any other post and
I can't see any reason why it shouldn't go to this function,
particularly if it works for a regular (non-AJAX) post, so any help
would be appreciated,
Paul
 
T

Tim Roberts

PJ said:
I have a simple web server using BaseHTTPServer, and the def do_POST
(self) function works fine for regular forms that are submitted to it,
but when I send an AJAX POST to it it does nothing (I've tried to just
get it to print to check it's nothing else but it doesn't even do
that, although it does for a regular POST request.

Are you absolutely sure your AJAX request is being sent with Content-Type
multipart/form-data? Your code would explode silently if it didn't,
because "query" would not exist when you got to "print(query.get('data'))",
and your blanket "except" would hide the error.
 
P

Paul

Are you absolutely sure yourAJAXrequest is being sent with Content-Type
multipart/form-data?  Your code would explode silently if it didn't,
because "query" would not exist when you got to "print(query.get('data'))",
and your blanket "except" would hide the error.

Thankyou for that - you're right - I'm new to AJAX so it's taking me a
bit of time to get used to it - its an application data type being
submitted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,052
Members
48,769
Latest member
Clifft

Latest Threads

Top