weixin_33693070 2017-01-26 17:41 采纳率: 0%
浏览 31

在AJAX调用中读取POST数据

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?

  • 写回答

1条回答 默认 最新

  • ??yy 2017-01-27 18:23
    关注

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

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏