Jump to content
Larry Ullman's Book Forums

Recommended Posts

Using Ajax, I want to use the POST method with https://

example:

ajax.open("post","https://example.com/submit.php", true));

ajax.send("name=John");

 

What I want to know is: will the POST data (in this case, John) be encrypted as it is sent to submit.php?

 

----------------------------------------------

 

I would also like to know: should ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded"); be placed between ajax.open() and ajax.send() if I use the POST method? Another site says to use it.

Link to comment
Share on other sites

I always place ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded"); between the open and send methods. I think the post won't work otherwise, but don't quote me on that.

 

Also, I am assuming that the data is properly encrypted, just like a standard post request, but again, don't quote me on that.

 

Sorry I couldn't be of more help. I trust someone will provide a real answer soon enough.

Link to comment
Share on other sites

  • 1 month later...

Okay, thank you. So now I have another question: In the "PHP 5 Advanced 2nd edition" book on page 526, Larry says that "using POST in your Ajax won't work properly with the PHP script".

 

1. What's the actual problem with using POST to send data from Ajax to PHP script? So far my testing has worked - PHP received the POST data.

2. Should I only use GET with Ajax?

 

Thank you! These forums have given me a lot of help.

Link to comment
Share on other sites

I don't have the book in front of me, but I suspect I write that using POST in the Ajax won't work with the PHP script because the PHP script expects data to be transmitted via GET. If you use POST in Ajax, you just need to change the PHP script accordingly.

 

There's no reason you can't use POST with Ajax, but the decision on GET vs. POST should be the same as any time you're deciding between the two (i.e., POST is meant to affect changes or actions on the server; GET is intended to retrieve information).

 

Thanks for the nice words on the forums!

Link to comment
Share on other sites

Thank you. After thinking a little more on the topic, I believe that the reason why I have no problems using POST with Ajax is because I add this line of code:

 

ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");//to those who want to use this line of code: remember to replace the variable "ajax" with your variable name

 

Which will then send the POST data just like a normal HTML form!

Link to comment
Share on other sites

 Share

×
×
  • Create New...