【问题标题】:jQuery $.ajax post doesn't posts any data to ASP pagejQuery $.ajax post 不会将任何数据发布到 ASP 页面
【发布时间】:2011-05-13 20:54:59
【问题描述】:

这是代码:

表单的HTML:

<div id="contact_form">
<h1>Contact us</h1>
<p>Please use the form below to send us a message.</p>
<form method="post" onsubmit="return sendContact();" action="sendContact.asp">
<p><label for="name" id="lname">Full name:</label>
<input type="text" class="text" name="name" id="name" onfocus="input_focus('name');" onblur="input_blur('name');" /></p>
<p><label for="email" id="lemail">Email address:</label>
<input type="text" class="text" name="email" id="email" onfocus="input_focus('email');" onblur="input_blur('email');" /></p>
<div class="x"></div>
<p class="error" id="email-error">You must enter your email address.</p>
<p><label for="category" id="lcategory">Category:</label>
<select name="category" id="category" onfocus="input_focus('category');"     onblur="input_blur('category');">
<option value="software">Software</option>
<option value="hardwae">Hardware</option>
<option value="consulting">Consulting</option>
<option value="resources">Resources</option>
</select></p>
<p><label for="message" id="lmessage">Message:</label>
<textarea name="message" id="message" onfocus="input_focus('message');"     onblur="input_blur('message');"></textarea></p>
<div class="x"></div>
<p class="error" id="message-error">You must enter your message.</p>
<p><label for="captcha" id="lcaptcha"></label>
<input type="text" class="text" name="captcha" id="captcha"     onfocus="input_focus('captcha');" onblur="input_blur('captcha');" /></p>
<div class="x"></div>
<p class="error" id="captcha-error">Are you shure about your calculations?</p>
<script type="text/javascript">
generate_captcha('lcaptcha');
</script>
<div class="x"></div>
<input type="submit" class="submit" name="send_contact" value="Send" />
</form>
<span id="contact-back">or you can <a href="index.html" class="read-more">Go back</a</span>
</div>
<div id="message_sent" style="display:none;">
<h1>Your message has been sent</h1>
<p>We'll contact you in a shortest possible time.</p>
<p>You can now <a href="index.html" class="read-more">go back</a> to home page.</p>
</div>    
</div>

这是发布帖子的 js(sendContact() 函数的一部分):


     var data = $("#contact_form > form").serialize();

$.ajax({ type: "POST", url: "sendContact.asp", global: false, contentType: "application/x-www-form-urlencoded; charset=UTF-8", data: data, dataType: "text", async: false, cache: false, error:function () { alert('AJAX Connectivity Error');}, success: function(msg){ } });

$("#contact_form").fadeOut(1000, function() { $("#message_sent").slideDown(500); });

return false; }

这是应该捕获查询字符串“sendContact.asp”的代码:

&lt;%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%&gt;
&lt;%
Response.Buffer = False Response.CacheControl = "无缓存" 常量 ForReading = 1,ForWriting = 2,ForAppending = 8 设置 Fso = server.CreateObject("Scripting.FileSystemObject") 设置 SendContact = Fso.OpenTextFile(Server.MapPath("\contact.txt"), ForAppending, True, -1) SendContact.WriteLine "咨询:"&Request("category")&",时间戳:"&Now()&"" SendContact.WriteLine "IP:"&Request.ServerVariables("REMOTE_ADDR")&"" SendContact.WriteLine "名称:"&Request("name")&"" SendContact.WriteLine "电子邮件:"&Request("email")&"" SendContact.WriteLine "Mensaje: "&Request("message")&"" SendContact.WriteBlankLines 2 SendContact.Close %> 当信息与表单一起发送时,一切看起来都是正确的,但是任何数据都被发送到“senContact.asp”,并且除了时间戳和远程 IP 之外,“contact.txt”的字段中写入了一个空格。我也尝试过请求“Request.Querystring(“id”)”,但结果是一样的。

感谢您的游览帮助

【问题讨论】:

  • 嗯,这需要基本的调试。以data 开头 - 它包含任何内容吗?试试console.log(data)找出答案
  • 数据发送正确,我认为问题可能出在 ASP 页面中。
  • 我复制了您的代码,它工作得很好.. 有两件事浮现在脑海中。 a)可能是一个页面经过身份验证而另一个页面没有? b)您是否使用任何读取提交的二进制数据的组件?因为我记得其中大多数都以某种方式混淆了发布的数据,并且通常会创建一个包装器对象来提供该信息。

标签: jquery post asp-classic


【解决方案1】:

虽然这是一个老问题,但我正在发送它可能的答案。

在绑定每个 POST 参数之前,您可以使用 escape 函数对变量进行编码并转换空格和任何其他“不安全”字符以供 URL 使用。

举个简单的例子(我在这里只使用了一个参数。您可以相应地添加其他参数):

$.ajax({
  type: "POST",
  url: "sendContact.asp",
  global: false,
  contentType: "application/x-www-form-urlencoded; charset=UTF-8",
  data: "name=" + escape(document.getElementById("name").value),
  dataType: "text",
  async: false,
  cache: false,
  error:function () { 
   alert('AJAX Connectivity Error');}, 
  success: function(msg){
  }
});

希望它对你有用。

【讨论】:

    【解决方案2】:

    两个想法:

    验证

    您在这一行上有一个未关闭的&lt;/a 标记(您可能需要将代码向右滚动一点):

    <span id="contact-back">or you can <a href="index.html" class="read-more">Go back</a</span>
    <!--                                                                                ^-- here -->
    

    W3C validator 可以帮助解决这类问题。无效的标记真的会浪费你的时间。 :-) 但这很可能是一个红鲱鱼。 (验证器还说有一个额外的&lt;/div&gt;,但我怀疑这只是复制粘贴的产物。)

    提交处理程序中的同步 Ajax

    我不会尝试从表单提交处理程序中发出 同步 Ajax 请求,这看起来就像你在自找麻烦。

    我会尝试让事件处理程序完成,然后然后发送表单:

    function sendContact() {
        var data = $("#contact_form > form").serialize();
    
        setTimeout(sendViaAjax, 0);
        return false;
    
        function sendViaAjax() {
    
            $.ajax({
              type: "POST",
              url: "sendContact.asp",
              global: false,
              contentType: "application/x-www-form-urlencoded; charset=UTF-8",
              data: data,
              dataType: "text",
              async: false,
              cache: false,
              error:function () { 
                  alert('AJAX Connectivity Error');
              }, 
              success: function(msg){
              }
            });
    
            $("#contact_form").fadeOut(1000, function() {
                $("#message_sent").slideDown(500);
            });
        }
    }    
    

    题外话,但我强烈建议避免在主 UI 线程上进行同步 Ajax 请求。 (web worker 中的同步 Ajax,很好。)不需要它们,它们会锁定浏览器的某些东西(尤其是 IE,但它们都在不同程度上)。

    【讨论】:

    • 未关闭的链接是错误的,在这里粘贴代码,我检查了所有标签。我将请求设为同步只是为了进行调查,但无论是同步的还是异步的,它都不起作用。
    【解决方案3】:

    内容类型默认为application/x-www-form-urlencoded,数据始终以utf-8 发送。

    不确定这条线

    contentType: "application/x-www-form-urlencoded; charset=UTF-8"
    

    应该包含两者..尝试不使用它,以防它弄乱正在发送的数据


    原始答案被 T.J. 证明是错误的。人群

    您正在使用POST 方法发送数据,因此您需要使用request.form("..") 读取它们

    所以把你所有的request("..")改成request.form("..")

    参考:http://msdn.microsoft.com/en-us/library/ms525985%28VS.90%29.aspx

    【讨论】:

    • 不,在 ASP 中,Request("x")GETPOST 数据中获取“x”变量,无论它在哪里找到。您可以更具体(Request.Form("x") 代表POST),但您不必如此。
    • @T.J.克劳德,你是对的……只是以同样的方式做事的力量,让你忘记了替代方案……
    【解决方案4】:

    您是否使用过 fiddler 之类的应用来查看访问该页面的流量,以确保数据确实被发布了?还将淡出表单和显示 div 移到成功处理程序中,您只希望它在成功的 ajax 请求上运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-18
      • 2014-12-19
      相关资源
      最近更新 更多