3

I have some Session values that I am constantly changing via Ajax calls. I can't seem to get a handle on the POST data to process it and set the values.

What I am passing to it here is an array of strings like is shown in my code below.

Here is where AJAX calls:

var sessionValues = [];
str = {"PID": "1", "Level": "Main", "MenuName": "Kitchen", "State": "CHECKED"}
sessionValues.push(str);
var postObj = {"sessionData": sessionValues};

 $.ajax({
  type: 'POST',
  data: {'data': postObj},
  url: 'setSession.asp'
 }).done(function(response){
 console.log(response);
})

I have this working fine in a PHP version of the program but my ASP version is not grabbing the data. Here is my PHP ver and the ASP ver as best as I could convert it.

<-- php setSession.php works fine -->
$data = $_POST['data'];
foreach ($data['sessionData'] as $key => $value) {
 $projectProduct = "1";
 $level = $value["Level"];
 $menuName = $value["MenuName"];
 $state = $value["State"];
 $_SESSION['PID:'.$projectProduct][$level][$menuName]['menu_state'] = $state;
 echo "[PID:".$projectProduct."][".$level."][".$menuName."][".$state."]<br>";
}
0 =>>>>> Array<br>[PID:1][Main][Kitchen][CHECKED]

Here I want to do the same thing in ASP

' setSession.asp 
data = Request.Form("data")
For Each part In data("sessionData")
 projectProduct = part("PID")
 level = part("Level")
 menuName = part("MenuName")
 state = part("State")
 Session("PID:" & projectProduct).Item(level).Item(menuName).Remove("menu_state")
 Session("PID:" & projectProduct).Item(level).Item(menuName).Add "menu_state", state
response.write("[PID:" & projectProduct&"]["&level&"]["&menuName&"]["&state&"]<br>")
Next

outputs blank

It looks like it never has any data but doesn't throw any errors. Am I reading the POST object correctly?

[edit]

Here is the RAW POST data captured from Fiddler:

data%5BsessionData%5D%5B0%5D%5BPID%5D=1&data%5BsessionData%5D%5B0%5D%5BLevel%5D=Main&data%5BsessionData%5D%5B0%5D%5BMenuName%5D=Kitchen&data%5BsessionData%5D%5B0%5D%5BState%5D=CHECKED

here I used a URL Decode on that string-

data[sessionData][0][PID]=1&data[sessionData][0][Level]=Main Level Plan&data[sessionData][0][MenuName]=Kitchen&data[sessionData][0][State]=CHECKED

This looks like I should be able to loop through the strings now by using

For Each part In Request.Form("data[sessionData]")

but nothing happens. I added a simple loop to look at the request.form and here is what it is seeing:

for each x in Request.Form
    Response.Write(x)
Next
' outputs -> data[sessionData][0][PID]data[sessionData][0][Level]data[sessionData][0][MenuName]data[sessionData][0][State]

I guess what this comes down to is just reading through and processing that string correctly, or multiple if more than one is sent. Correct?

user692942
  • 16,398
  • 7
  • 76
  • 175
user1601513
  • 153
  • 2
  • 10
  • 1
    It would help if we could see the RAW HTTP POST being sent to the ASP page, can you capture using Fiddler? – user692942 Jan 26 '17 at 23:26
  • This is all that Fiddler sends - RESPONSE BYTES (by Content-Type) -------------- ~headers~: 252 text/html: 20 – user1601513 Jan 27 '17 at 05:13
  • and these are the first lines from Fiddler - Request Count: 1 Bytes Sent: 1,446 (headers:1,240; body:206) Bytes Received: 272 (headers:252; body:20) – user1601513 Jan 27 '17 at 05:20
  • That is just the "Statistics" tab, to see the RAW request and response bring up the "Inspector" tab and in the bottom panel select "RAW" then copy and paste that content into a code block *(`{ }`)* with an [edit] to the question, don't try posting it in the comments. – user692942 Jan 27 '17 at 10:52
  • 2
    One thing to keep in mind is that square brackets are not used by VBScript code - there's no command, function, or operator that is written with square brackets. (You *can* use square brackets in your own variable names, but they're just like any other character: they don't magically make a variable into an array.) Thus, if you find yourself writing a square bracket that's not enclosed in quotation marks, chances are, you're making a mistake. – Martha Jan 28 '17 at 16:13

1 Answers1

3

The RAW output definitely helps work out what is going on.

What is happening is jQuery is translating the JSON structure into HTTP POST parameters but during the process, it creates some overly complex key names.

If you break down the key value pairs you have something like

data[sessionData][0][PID]=1
data[sessionData][0][Level]=Main Level Plan
data[sessionData][0][MenuName]=Kitchen
data[sessionData][0][State]=CHECKED

As far as Classic ASP is concerned the this is just a collection of string key and value pairs and nothing more.

The correct approach to work out what these keys are is to do what you have done in the question, but with some minor alternations.

For Each x In Request.Form
    Response.Write(x) & "=" & Request.Form(x) & "<br />"
Next

Which when outputted as HTML will look similar to the break down shown above.

Armed with the knowledge of what the keys are you should be able to reference them directly from the Request.Form() collection.

Dim pid: pid = Request.Form("data[sessionData][0][PID]")
Response.Write pid

Output:

1
user692942
  • 16,398
  • 7
  • 76
  • 175
  • Still not able to access the other values that are passed by reference to the Key names like this response.write("Level => " & part["Level"]). Throws an Internal Server Error – user1601513 Jan 27 '17 at 20:35
  • 1
    @user1601513 I thought I explained what the keys are? The value of `level` is stored in the key `data[sessionData][0][Level]` so to output it you would use `Response.Write Request.Form("data[sessionData][0][Level]")`. Remember the key is just a string value nothing is inferred, it's just a string that is used as the key to identify a specific piece of data that was posted. – user692942 Jan 28 '17 at 00:22
  • but now this here outputs "[Level]" but what I need to get is "Main". pid = Request.Form("data[sessionData][0][Pid]") level = pid & "[Level]" response.write(level) – user1601513 Jan 28 '17 at 10:56
  • 1
    @user1601513 I've explained how to access the keys and their associated values. I'm sorry but I just don't understand what you are trying to do from the code you keep posting `Main` but the only `"Main"` is part of the value returned by the key `"data[sessionData][0][Level]"`, the whole value is `"Main Level Plan"`. – user692942 Jan 28 '17 at 11:26
  • 2
    Sorry for the confusion. I am on a high level of cancer drugs for pain. I am now accessing the POST data as I need to thanks to you. Thanks for all the help and patience with me. – user1601513 Jan 28 '17 at 17:01
  • 1
    @user1601513 no problem, don't worry about it...just glad to help. Good luck with everything. – user692942 Jan 28 '17 at 18:16