![]() |
AJAX code confusion
Hello everyone,
I bought a book and began learning how to use AJAX and my friend gave me a practice assignment where I have to enter values into one div and perform an AJAX get request to a website she gave me to receive the information in the right div. Maybe I am doing the AJAX get request incorrectly following this book. If anyone could point me in the right direction I would appreciate it. This is not for school, and I have no experience in web scripting languages but willing to learn! Just give my javascript a look and maybe you can help me out. I am not sure yet on how to display the information after it has been received but I know for sure I do not want to load another page when hitting the submit button! Also, I have a weird way of lining up my code from programming in C++ for about 4 years now. I have developed it over time and sorry if it is hard to understand. If so, then I can format it a proper way and re-post. Also, I am trying to learn how to use the AJAX GET method, not the POST method. And I want to return the information in JSON. Code:
<html> |
The biggest problem there is that you appear to be using a word processor to write your code, and so you are *NOT* getting ordinary " characters.
Look at the difference: Yours: “POST” Correct: "POST" Yours: ”name=” Correct: "name=" JavaScript will *NOT* accept those word processing quote marks! NEVER use a word processor for writing code! Use an editor designed for coding. ******* And then you are mixing up GET and POST. First of all, you need to escape the values of all the parameters, no matter if you are using GET or POST: Code:
var parameters = "name=" + encodeURIComponent(t2lName) Code:
myAjaxGetrequest.open("POST", “http://xxx.zzz.com/page.zzz", true);Code:
myAjaxGetrequest.open("GET", "http://xxx.zzz.com/page.zzz?" + parameters, true); |
regarding confusion
I changed up my code till it was like yours. I am not sure why it posted like it did, but I use notepad++ in html format for editing. If this is incorrect please let me know. I think that when I posted it, it changed how things look, because my quotation marks are the same as yours. I do not want my submit button to open a new page, I want it to synchronously update the div on the right side to display the information. Thanks for the help that you provided me with though, I am closer now than before. I posted my updated code on pastebin.com at the below link (it looks as you described it should, yet it still doesn't look like it should but it is readable);
On pastebin: http://pastebin.com/2WNrJJaM And, the same code here; Code:
<html> |
? So does it or does it now work now?
But wait a minute! Code:
if(myAjaxGetrequest.status==200 || window.location.href.indexOf("http")==-1){That makes no sense. *ANY* location.href will contain "http" unless you are using "file://" for use on your own machine, only. On top of that, your code isn't waiting for the AJAX response! You wrote that you wanted "to synchronously update the div". Did you mean that? You really want SYNCHRONOUS instead of the normal ASYNCHRONOUS? If so, you need to use false as the last argument to the open() and then you do NOT need to check readystate or status. Make up your mind. Either: Code:
// SYNCHRONOUS requestCode:
// ASYNCHRONOUS requestNOTE: You could use the try/catch for errors with the synchronous code, as well. And/or you could check for a status other than 200, but that's seldom needed. |
regarding confusion
When I click my submit button, it just takes me to a page that says "File not found". I do not understand why it would do this. I want to click the submit button and it issue the request with the information and display the information in the div on the right. I changed the code to what you had there (synchronous). Maybe I am doing the entire code wrong. Something weird is going on, any ideas on what I should do? Thanks for the help so far.
Code:
<html> |
You still have not
fixed all the quote marks and you have one too many } also you are trying to access form elements by name but they only have id's |
quotes
The quotes look like they should in my notepad++. I am not sure why they are changing when in the code tags of this site. What do you mean by name and id? I am new to this. Should it be name instead of id? Where is the } that should not exist?
|
Quote:
<script type=”text/javascript”> return new ActiveXObject(“Microsoft.XMLHTTP”); document.getElementById(“testForm”).style.display = “none”; <a href=”#” onclick=”submitFormWithAjax();”> Quote:
form elements usually need names. this uses the form element name ... document.testForm.ebox.value Quote:
Code:
<script type=”text/javascript”>element but the form tag has a name ... document.getElementById(“testForm”).style.display = “none”; |
The OP indicated that he "use(s) notepad++ in html format for editing". Shouldn't he be editing in code format?
|
| All times are GMT +1. The time now is 05:02 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.