简体   繁体   English

AJAX - 经典 ASP - 发布表单

[英]AJAX - Classic ASP - Post a Form

I have a TEST.ASP with this code:我有一个带有以下代码的 TEST.ASP:

<HTML>
<HEAD>
    <SCRIPT src="ajaxScript.js" type="text/javascript"></SCRIPT>
</HEAD>
<BODY>
    <FORM action="action_page.asp" method="post">
        First Name:<BR>
        <INPUT type="text" name="FName"><BR>
        Last Name:<BR>
        <INPUT type="text" name="LName"><BR>
        <INPUT type="submit" value="Submit">
        <BUTTON type="button" onClick="loadXMLDoc('action_page.asp',this.form);">GoGoGo!</BUTTON>
    </FORM>
    <DIV id="msgBoxDiv">TEST!!</DIV>
</BODY>
</HTML>

The Javascript file that is called (ajaxScript.js) has this code:被调用的 Javascript 文件 (ajaxScript.js) 具有以下代码:

var req; // global variable to hold request object

function processReqChange()
{
    if (req.readyState == 4 && req.status == 200){document.getElementById("msgBoxDiv").innerHTML = req.responseText;}
}

function loadXMLDoc(url, params)
{
    if(window.XMLHttpRequest)
    {
        try
        {
            req = new XMLHttpRequest();
        } catch(e)
            {
                req = false;
            }
    }
    else
    {
        req = false;
    }

    if(req)
    {
        var formData = new FormData(params);

        req.onreadystatechange = processReqChange;
        req.open("POST", url, true);
        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        req.send(formData);
        return true;
    }
    return false;
}

And my "action_page.asp" to receive the parameters is like so:我的“action_page.asp”接收参数是这样的:

<%
    vRF1 = request.Form("FName")
    vRF2 = request.Form("LName")
%>

<HTML>
    <HEAD>

    </HEAD>
    <BODY>
        First:<%=vRF1%><BR>
        Last:<%=vRF2%>
    </BODY>
</HTML>

If I make the normal submit (submit button), everything goes as expected: it shows a new page with the values of the form.如果我正常提交(提交按钮),一切都会按预期进行:它显示一个包含表单值的新页面。

BUT... if I try to read the target ASP with AJAX (gogogo button), I can't send the form to the target page.但是...如果我尝试使用 AJAX(gogogo 按钮)读取目标 ASP,我无法将表单发送到目标页面。 I do receive the target page, but without the supposed values.我确实收到了目标页面,但没有预期的值。 I get this:我明白了:
Result page结果页

If I change "req.send(formData);"如果我更改“req.send(formData);” for " req.send("FName="+1+"&LName=QWER");", everything works well.对于“ req.send("FName="+1+"&LName=QWER");”,一切正常。

I've read that to send the entire form (like the "usual" post do), I just need to make "var formData = new FormData(params);"我已经读过要发送整个表单(就像“通常的”帖子一样),我只需要制作“var formData = new FormData(params);” where params would be the form object, and then send the FormData(params).其中 params 将是表单对象,然后发送 FormData(params)。

What may I be doing wrong here?我可能在这里做错了什么?

I didn't want to leave this without an answer.我不想在没有答案的情况下离开这个。

Lankymart gave the path to the right answer... the truth is, when I submitted the "new FormData(formID)" I was sending in multipart/form-data and not application/x-www-form-urlencoded as I was declaring. Lankymart 给出了正确答案的路径......事实是,当我提交“新 FormData(formID)”时,我发送的是 multipart/form-data 而不是 application/x-www-form-urlencoded,因为我正在声明.

SO... We can make the hypothesis Lankymart indicated (application/x-www-form-urlencoded).所以...我们可以做出 Lankymart 指出的假设(application/x-www-form-urlencoded)。 In other words, you just take my initial question, and in req.send();换句话说,你只需要回答我最初的问题,然后在 req.send(); 中。 you have to form the string of the parameters to pass (just like in a GET request).您必须形成要传递的参数字符串(就像在 GET 请求中一样)。 "name1="+param1+"&name2="+param2. "name1="+param1+"&name2="+param2. I opted by this one, because, for what I needed, this was more than enough.我选择了这个,因为,对于我所需要的,这已经绰绰有余了。

OR... If we do need/wish to send the form - req.send(formData)... that will produce a multipart/form-data (check an example of the format of a multipart/form-data here: http://www.codeproject.com/Articles/1125/Advanced-ASP-Uploader ) In this case... you have to build a parser for the info.或者...如果我们确实需要/希望发送表单 - req.send(formData)... 这将产生一个 multipart/form-data(在这里查看 multipart/form-data 格式的示例: http ://www.codeproject.com/Articles/1125/Advanced-ASP-Uploader )在这种情况下......你必须为信息构建一个解析器。 Here is what I've coded (just to try a thing or two):这是我编写的代码(只是为了尝试一两件事):

Function StoreNameAndValues(tempVarArray)
    Dim tempVar

    for i = 1 to ubound(tempVarArray)-1
        if tempVarArray(i)<>"" then
            tempVar=Split(tempVarArray(i), """")

            if ubound(tempVar)>1 then
                postNamesArray(i-1)=tempVar(1)
                postValuesArray(i-1)=StripString(tempVar(2),CHR(13)&CHR(10))
            End if
        End if
    next
End Function


tempVar=Request.TotalBytes
tempVar1=Request.BinaryRead(tempVar)

tempVar1=SimpleBinaryToString(tempVar1)

separator=Split(tempVar1, CHR(13)&CHR(10))(0)
tempVar2=Split(tempVar1,separator)

postArgumentsSize=ubound(tempVar2)-1
Dim postNamesArray()
Dim postValuesArray()
ReDim Preserve postNamesArray(postArgumentsSize)
ReDim Preserve postValuesArray(postArgumentsSize)

StoreNameAndValues(tempVar2)

In this example, I've build a very rudimentary parser... it's not prepared for things like file upload, but this is just an example of what to do.在这个例子中,我构建了一个非常基本的解析器......它没有为文件上传之类的事情做好准备,但这只是一个例子。

Hope I didn't make any big mistake... and it will help someone.希望我没有犯任何大错误......它会帮助某人。

After searching everywhere and trying various things this post finally got me on the right track.在到处搜索并尝试各种事情之后,这篇文章终于让我走上了正轨。 My classic asp page simply wouldn't read the ajax form post that I was sending to it.我的经典 asp 页面根本无法读取我发送给它的 ajax 表单帖子。

What finally made it work is the contentType: 'application/x-www-form-urlencoded; charset=UTF-8',最终使它起作用的是contentType: 'application/x-www-form-urlencoded; charset=UTF-8', contentType: 'application/x-www-form-urlencoded; charset=UTF-8', in the ajax function. contentType: 'application/x-www-form-urlencoded; charset=UTF-8',在ajax函数中。 So this is how I finally got to send the form and the classic asp page reads all the values like it should.所以这就是我最终发送表单和经典 asp 页面读取所有值的方式。

    $('#subdel').click(function () {
      // get values outside of the form
      var tDate = $('#datepicker').val();
      var tCID = $('#Client').val();
      var vFD = $('#dataentry').serialize();
      // add those values to the form
      vFD = vFD + '&C=' + tCID;
      vFD = vFD + '&D=' + tDate;
      $(function() {
         var request = $.ajax({
            url: 'api/saveDLR.asp',
            type: 'POST',
            data: vFD,
            processData: false,
            contentType: false,
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            cache: false,
            success: function (response) {
               // hide some parts of the page, and then show the result from saving the form
               $('#cldel').hide("slow");
               $('#endel').hide("slow");
               $("#encon").html( response );
               $('#encon').show("slow");
               // refresh a table after the form was processed
               $(function() {
                   var request = $.ajax({
                   url: "/api/getDelClient.asp?C=" + tCID + "&D=" + tDate,
                   type: "GET",
                   dataType: "html",
                   success: function (response) {
                      $("#cldel").html( response );
                      $('#cldel').show("slow");
                   },
                   error: function (response) {
                      alert(response.responseText);
                   }
                   });
               });
            },
            error: function (response) {
                  alert(response.responseText);
            }
         });
      });
   });

Maybe it (still) can help somebody else.也许它(仍然)可以帮助其他人。

The asp page simply reads the form data with Request.Form asp页面只是简单地用Request.Form读取表单数据

<%  For Each item In Request.Form
      Response.Write item & " - " & Request.Form(item) & "<br/>"
    Next
%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM