This is a discussion on "[SOLVED] AJAX call to a servlet" within the JavaScript Forum section. This forum, and the thread "[SOLVED] AJAX call to a servlet are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
[SOLVED] AJAX call to a servlet
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
|||
|
[SOLVED] AJAX call to a servlet
Hi all,
I try to asynchronously pass a request to a servlet using the XMLHttpRequestObject. When I use method GET in open(), the request seems to be sent, but the servlet receives an empty(null) request and it returns a default response (not the required one). When I use method POST, I also try to set the request header using: XMLHttpRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); However, in this case the XMLHttpRequestObject.status = 405. Hence, I'm afraid the request header is not set properly - maybe I set it the wrong way. When I try to display the response headers using: document.write("Get All Response Headers: " + XMLHttpRequestObject.getAllResponseHeaders); ...the output is: Get All Response Headers: function getAllResponseHeaders() { [native code] } If I try to display the changes in the XMLHttpRequestObject status, I get the following output: OPENED state (1) HEADERS RECEIVED state (2) LOADING state (3) DONE state (4) state: 1 HEADERS RECEIVED state (2) LOADING state (3) DONE state (4) state: 2 LOADING state (3) DONE state (4) state: 3 LOADING state (3) DONE state (4) state: 3 LOADING state (3) DONE state (4) state: 3 LOADING state (3) DONE state (4) state: 3 LOADING state (3) DONE state (4) state: 3 LOADING state (3) DONE state (4) state: 3 DONE state (4) state: 4 I read the XMLHttpRequestObject documentation: http://www.w3.org/TR/XMLHttpRequest/#xmlhttprequest, but I still cannot figure out what the problem is. I'll be happy if you could give me any hint about what could be wrong or how I can continue debugging. I already ran out of ideas. Thanks, Pesho |
|
|
|
||||
|
Re: AJAX call to a servlet
Okay can you show the code you are using? It seems that you are doing the right thing
All 405 errors can be traced to configuration of the Web server and security governing access to the content of the Web site, so should easily be explained by your ISP....
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
|
|
|||
|
Re: AJAX call to a servlet
thanks Rakuli,
ok, here is my javascript code:
!!! However, as I already said, when I use GET method the servlet gets an empty request. When I use the POST method, there is some problem with the request headers, because HTTP status of the returned page is 405. How can I exactly trace the HTTP 405 error? I'm using a local Tomcat container on Windows. Thanks |
|
||||
|
Re: AJAX call to a servlet
I'm sorry I don't know anything about Tomcat
As for the GET request -- you have your query string escaped... Is there a reaso for this? It looks like a query string with = reaplaced -- if you want to set GET string you need to unescape the = and & characters otherwise it is just read as a long string. eg.. XMLHttpRequestObject.send(decodeURI(newURL)); Is there reason you are sending it as a long string or do you actually want there to be numerous variables.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
|
|
|||
|
Re: AJAX call to a servlet
Quote:
After your advice, I tried to simplify the URI to: key1=value1&key2=value2&key3=value3 In turn, I output the query string from the servlet as follows:
value1Parameter= null QueryString= null content type: null no idea what is going on! |
|
||||
|
Re: AJAX call to a servlet
Okay, I just realised what is wrong with the script..
Try this
Cheers
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
|
|
|||
|
Re: AJAX call to a servlet
hey Rakuli,
I can't find words to thank you ! ! ! ! ! ! ! Yes, that was the problem indeed! I was so stupid not to pay attention to this detail. My brain was already in a BLOCKED state can I ask you one more thing. Now it's working as expected, but only with a hardcoded URL. When I pass a dynamic URL to the JavaScript function I get errors. Here you can see the difference between the hardcoded URL, which works OK, and the one I get ONCLICK as a parameter of the function: hardcoded URL(works as expected): pageasset=centercontent&fh_params=fh_reffacet%3Dca tegories%26fh_eds%3D%25c3%259f%26fh_refview%3Dsumm ary%26fh_refpath%3Dfacet_1%26fh_location%3D%252f%2 52fcatalog01%252fen_US%252fcategories%253c%257bcat alog01_15000%257d%252fcategories%253c%257bcatalog0 1_15000_15085%257d%252fcategories%253c%257bcatalog 01_15000_15085_15894%257d%26fh_start_index%3D10 dynamic URL, taken ONCLICK: pageasset=centercontent&fh_params=fh_reffacet=cate gories&fh_eds=%c3%9f&fh_refview=summary&fh_refpath =facet_1&fh_location=%2f%2fcatalog01%2fen_US%2fcat egories%3c%7bcatalog01_15000%7d%2fcategories%3c%7b catalog01_15000_15085%7d%2fcategories%3c%7bcatalog 01_15000_15085_15894%7d&fh_start_index=20 obviously, the fh_params value should be encoded as in the first URL. Can you tell me how to do it in JavaScript? THANKS again!!! Pesho |
|
||||
|
Re: AJAX call to a servlet
yes, encode with encodeURIcomponent.. so if you are building the link, pass the string that needs to be sent encoded using encodeURIcomponent
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
|
|
|||
|
Re: AJAX call to a servlet
respect man!
encodeURIcomponent didn't actually work for me, but I used escape(). Anyway, thank you for the hint. Now I only have to handle a problem with IE - it doesn't refresh the page after the response is received. But I'll try to resolve this problem... Thank you once again ! Best, Pesho |
|
||||
|
Re: AJAX call to a servlet
No problems... Have fun with it -- I love ajax
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
|
![]() |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [solved] Don't know what to call it | acrikey | Webforumz Cafe | 3 | Mar 24th, 2008 18:45 |
| problem in IE after asynchronous (AJAX) call | pesho318i | Web Page Design | 3 | Mar 21st, 2008 15:17 |
| Call PHP Web Service With Ajax | 2mk | PHP Forum | 5 | Feb 9th, 2008 22:02 |
| [SOLVED] preserve the state of checkboxes after an AJAX call | pesho318i | JavaScript Forum | 4 | Nov 7th, 2007 16:35 |
| AJAX call function | melvinoyh | JavaScript Forum | 2 | May 31st, 2006 01:02 |