在AJAX调用中读取POST数据

时间:2022-01-14 05:10:20

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.

我有一些Session值,我通过Ajax调用不断更改。我似乎无法处理POST数据来处理它并设置值。

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

我在这里传递的是一个字符串数组,如下面的代码所示。

Here is where AJAX calls:

这是AJAX调用的地方:

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版本中工作正常但我的ASP版本没有抓取数据。这是我的PHP版本和ASP版本,因为我可以转换它。

<-- 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

在这里,我想在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?

看起来它从来没有任何数据,但不会抛出任何错误。我正确地读了POST对象吗?

[edit]

Here is the RAW POST data captured from Fiddler:

这是从Fiddler捕获的RAW POST数据:

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-

我在这个字符串上使用了一个URL Decode-

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:

但没有任何反应。我添加了一个简单的循环来查看request.form,这是它所看到的:

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?

我想这归结为只是正确读取和处理该字符串,如果发送多个字符串,则为多个。正确?

1 个解决方案

#1


3  

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

RAW输出肯定有助于解决正在发生的事情。

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.

正在发生的事情是jQuery正在将JSON结构转换为HTTP POST参数,但在此过程中,它会创建一些过于复杂的密钥名称。

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.

就Classic ASP而言,这只是字符串键和值对的集合,仅此而已。

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.

哪个输出为HTML时看起来类似于上面显示的细分。

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

有了密钥的知识,您应该能够直接从Request.Form()集合中引用它们。

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

Output:

1

#1


3  

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

RAW输出肯定有助于解决正在发生的事情。

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.

正在发生的事情是jQuery正在将JSON结构转换为HTTP POST参数,但在此过程中,它会创建一些过于复杂的密钥名称。

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.

就Classic ASP而言,这只是字符串键和值对的集合,仅此而已。

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.

哪个输出为HTML时看起来类似于上面显示的细分。

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

有了密钥的知识,您应该能够直接从Request.Form()集合中引用它们。

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

Output:

1