jnroche Posted November 23, 2006 Share Posted November 23, 2006 i got a problem with my ajax code not working when i change the method to POST!i followed the very consistent solution on how to implement a POST method to no avail. Please help!!!function xmlhttprequest(id, url, method, keyvalue) { var xmlhttp = getxmlhttpobject(); if (!xmlhttp) { alert('Your browser does not support xmlhttp object.'); return; } xmlhttp.open(method, url, true); alert('method: '+method+' keyvalue: '+keyvalue); if (method=='POST') { xmlhttp.setRequestHeader("Content-Type", "application/x-www-urlencoded"); xmlhttp.setRequestHeader("Content-Length", keyvalue.length); xmlhttp.setRequestHeader("Connection", "close"); xmlhttp.send(keyvalue); } else { xmlhttp.send(null); } xmlhttp.onreadystatechange = stateChanged; return 1;}the middle of the code is where i placed the changes. the POSTED variables does not seem to contain any values at all!! Link to comment Share on other sites More sharing options...
jesh Posted November 27, 2006 Share Posted November 27, 2006 That's pretty much exactly the code I use to post data to my server using AJAX and it works fine for me.Here's what I use: http_request.onreadystatechange = handler; http_request.open("POST", url, true); http_request.setRequestHeader("Content-Type", "application/x-www-urlencoded"); http_request.setRequestHeader("Content-Length", data.length); http_request.setRequestHeader("Connection", "close"); http_request.send(data); I have three questions for you:1) Are you trying to POST this data to a different server than the server which is loading your current page? AJAX requests need to be within the same server (no cross-server scripting).2) What exactly is the data that you are trying to send? Sending something like "id=4,test=go,foo=bar" works for me.3) Do GET requests work for you? If not, you might try specifying the onreadystatechange handler before you call open on the xmlhttp object rather than after you send the request to the server. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now