0

I am having difficulties getting access to the values that I am sending to an ASP file from an HTML file. I am attempting to use AJAX POST method to send 3 values to a database and insert them, but to no avail.

Here is the .html code to send the values:

// Builds AJAX request and sends it to ASP page
function sendInfo(x,y,z){
    if (window.XMLHttpRequest){
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else{
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=stateChanged; 
    xmlhttp.open("POST","UpdateAJAX.asp",true);
    xmlhttp.send("addy="+(x)+
                  "&lat="+(y)+
                  "&lng="+(z));
}

// Checks the ready state of the sent response
function stateChanged() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
    else {
        document.getElementById("txtHint").innerHTML="Error with ready state: " + xmlhttp.readyState + " and status: " + xmlhttp.status;
    }
}

And here is the 'UpdateAJAX.asp' code:

<% 
    conn = Server.CreateObject("ADODB.Connection"); 
    conn.Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" +  Server.MapPath("Project.mdb"));

    ' var addy = Request.QueryString("addy");'
    ' var lat = Request.QueryString("lat");'
    ' var lng = Request.QueryString("lng");'

    var addy = Request.Form("addy");
    var lat = Request.Form("lat");
    var lng = Request.Form("lng");

    var sql="INSERT INTO Location_Info (Address,Latitude,Longitude)"
    sql= sql + " VALUES "
    sql= sql + "('" + addy + "',"
    sql= sql + "'" + lat + "',"
    sql= sql + "'" + lng + "')"
    Response.Write(sql);

    rs = conn.Execute(sql);

    Response.Write("Your record has been placed into the database.");

    conn.Close();
%>

I have tried both the Request.Form and Request.QueryString methods to retrieve the values from the send, but nothing gets returned. When I use Firefox Firebug to debug the code, this is what is being sent:

'addy=1232 Randall Avenue, Hammond, LA 70403, USA&lat=30.4545509&lng=-90.49790769999998'

It doesn't parse it into the individual variables either way I try and only returns error status of 500, which I have also debugged to mean:

'Data type mismatch in criteria expression.'

When attempting to send the sql statement, as the values are all undefined. If someone can provide any advice as to how to get the values into the variables, it would be greatly appreciated.

Cameron Fedy
  • 61
  • 1
  • 7

1 Answers1

0

You need to set the Content-Type header of your http request to application/x-www-form-urlencoded. Before you call xmlhttp.send(...), add this line:

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
gilly3
  • 87,962
  • 25
  • 144
  • 176