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.

5
  • You can try this stackoverflow.com/a/30083658/4773983, hope it helps Commented Jun 12, 2015 at 13:40
  • Do I just need to add '<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"> </asp:ScriptManager>' to the top of my ASP page? Commented Jun 12, 2015 at 13:51
  • Not just that, also change a little your JS code, like in the example and add the [WebMethod] in your static method of your code behind Commented Jun 12, 2015 at 13:54
  • @CameronFedy - Your code sure looks like Classic ASP, not ASP.Net. Your file has an ASP extension, whereas an ASP.Net file would have an ASPX extension. Are you working with Classic ASP or ASP.Net? Commented Jun 12, 2015 at 19:31
  • I wasn't aware of the difference, but i am strictly working with asp then, as I have a .asp extension Commented Jun 12, 2015 at 20:05

1 Answer 1

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");
Sign up to request clarification or add additional context in comments.

1 Comment

That worked! Thank you so much, this has been giving me such a headache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.