1

I am unable to access data in classic asp file sent from the jquery ajax call.

Here is my ajax call

$.ajax({
      type: "GET",
  url: "mymessage.asp?phone=" + $( "#mobile1" ).val() + "&message=" + $('textarea#message').val(),
    data:{ 

   phone: $( "#mobile1" ).val(),
   message: $('textarea#message').val()

   },
  contentType: "text/html; charset=utf-8",
  dataType: "text",
  success: function(msg) {
    // Replace the div's content with the page method's return.
    alert(msg);

  }
});

I am accessing the request values from asp page as

<%Request.Form("phone")%>

and

<%Request.Form("message")%>

but its not working.

I have debugged using Firefox firebug. The request is hitting the mymessage.asp page but I'm unable to access the request variables in the classic asp file

I am fed up after long search and various trial and hit methods. What is actually going wrong?? Please help.

P.S: there is nothing wrong in the data values as I have printed using console.log and data values are fine. Problem is with accessing the data in classic asp code. You can also see I kept both data and appended URL (although I tried them individually still not working)

user692942
  • 16,398
  • 7
  • 76
  • 175

2 Answers2

1

Since you are passing those values to asp page as querystring ( mymessage.asp?phone=" + $( "#mobile1" ).val() + "&message=" + $('textarea#message').val() ), you can not retrieve those values using Request.Form, as it is a GET request not POST Request. Also if you look into ajax method ,it is Get method - type: "GET",.

To access those values , you need to use Request.QueryString .

e.g. <%Request.QueryString("phone")%>

Read this - Difference between Request.Form and Request.QueryString?

Community
  • 1
  • 1
Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
  • Or change `type: "POST"`, in fact I would do this rather then passing values as "GET" requests. – user692942 Nov 17 '14 at 11:26
  • @Lankymart, in that case, for clarification sake, OP has to remove value from Querystring, as it is already passing through `data: {}` – Arindam Nayak Nov 17 '14 at 12:07
  • 1
    Absolutely let's be honest it's a mess. The OP should be not be passing querystring on the URL regardless of whether ajax request is a `GET` or `POST`, passing `data: {}` on a `GET` will get transformed to a querystring automatically no need to pass it manually like that. Allows for much easier switching between `GET` and `POST` requests. – user692942 Nov 17 '14 at 12:31
  • Not sure about that link it's the not the best...is there not a better quality one? – user692942 Nov 17 '14 at 14:32
1

@ArindamNayak is absolutely right.

GET and POST Knowing the Differences

HTTP GET requests passes the data as a serialised string of parameters separated by ampersand (&) delimiters appended to the request URL, also known as a querystring.

HTTP POST requests pass the data in the body of the request as serialised string of parameters in the same form as a GET querystring.

Classic ASP makes it easy to access these values with two collections

Request Collections

  • Request.QueryString() - Contains all parameters requested via the GET request method.

  • Request.Form() - Contains all parameters requested via the POST request method.

  • Request() - Contains all parameters requested via either the GET or POST request methods.

    NOTE: - This method is less efficient because the parameter has be checked in both the QueryString and Form collections, if you know where your data is coming from (whether it's a GET or POST request) it is best to use the corresponding collection.


Example Usage

If the following request is made

GET http://example.com/default.asp?phone=123456&message=test HTTP/1.1

In the page default.asp the parameters can be accessed using either the Request.QueryString() or Request() collections.

<%
  Dim message

  'Retrieves parameter from Request.QueryString collection.
  phone = Request.QueryString("message")
  'Checks both Request.Form and Request.QueryString.
  phone = Request("message")

  Response.Write phone 'Returns "test"
%>

If the following request is made

POST http://example.com/default.asp HTTP/1.1

phone=12345&message=test

In the page default.asp the parameters can be accessed using either the Request.Form() or Request() collections.

<%
  Dim message

  'Retrieves parameter from Request.Formcollection.
  phone = Request.Form("message")
  'Checks both Request.Form and Request.QueryString.
  phone = Request("message")

  Response.Write phone 'Returns "test"
%>

What About jQuery Ajax Requests?

jQuery makes a really easy to pass this information, the three properties you want to make sure are set correctly are;

  • url - The URL we are sending data to, do not include the querystring just the absolute / relative path to the Classic ASP page.

  • type - Either GET or POST depending on the intended request method.

  • data - The data we are sending to the URL. If it is type: "GET" the data will be automatically serialised and attached during the AJAX request as a URL querystring.

Using my previous example the AJAX request would look something like this;

$.ajax({
  type: "GET",
  url: "http://example.com/default.asp",
  data: {
    phone: "12345",
    message: "test"
  }
});

will create a RAW HTTP request like

GET http://example.com/default.asp?phone=12345&message=test HTTP/1.1

and in the Classic ASP page default.asp you can access the parameters using something like;

<%
  Dim phone, message
  phone = Request.QueryString("phone")
  message = Request.QueryString("message")
%>

Useful Links

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175