本文介绍了非AJAX jquery POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jquery POST函数,但它正在以AJAX样式处理请求。我的意思是它实际上没有进入我要告诉它的页面。

i am trying to use the jquery POST function but it is handling the request in AJAX style. i mean its not actually going to the page I am telling it to go.

$("#see_comments").click(function() {
            $.post(
            "comments.php",
            {aid: imgnum},
            function(data){

            });
        });

这个函数应该带有辅助值到comments.php页面。它的发布很好但没有重定向到comments.php。

this function should go to comments.php page with the aid value in hand. its posting fine but not redirecting to comments.php.

@Doug Neiner
澄清:

1我有15个链接(图片)。我点击一个链接,它加载我的JavaScript。 JS知道我打开了wat imgnum。我想在comments.php中找到这个imgnum。我必须使用这个JavaScript,没有其​​他方法可以做到这一点。 JS是强制性的。
2.您的方法成功发布援助值。但在comments.php中,当我尝试回显该值时,它什么也没显示。
3.我正在使用Firebug。在控制台中,它显示了我在步骤(2)中成功完成的回显REQUEST。

@Doug Neinerclarification:
1. I have 15 links (images). i click on a link and it loads my javascript. the JS knows wat imgnum i opened. this imgnum i want in the comments.php. i have to use this javascript and no other means can do the trick. the JS is mandatory.2. your method successfully POSTs the aid value. but in the comments.php when i try to echo that value, it displays nothing.3. I am using Firebug. in the Console, it shows the echo REQUEST i made in Step (2) successfully.

推荐答案

我知道你要做什么,但它不是你想要的。

I know what you are trying to do, but its not what you want.

首先,除非您在服务器上更改数据,否则请勿使用 POST 请求。只需 #see_comments 是正常的< a href ='/ comments.php?aid = 1'> ...

First, unless you are changing data on the server, don't use a POST request. Just have #see_comments be a normal <a href='/comments.php?aid=1'>...

如果 使用 POST ,那么这样做就可以了跟随您的通话的页面:

If you have to use POST, then do this to get the page to follow your call:

$("#see_comments").click(function() {
  $('<form action="comments.php" method="POST">' +
    '<input type="hidden" name="aid" value="' + imgnum + '">' +
    '</form>').submit();
});

这实际上是如何运作的。

第一个 $。post 一个AJAX方法,不能用来做传统的表单提交就像你描述的那样。因此,为了能够发布值导航到新页面,我们需要模拟表单帖子。

First $.post is only an AJAX method and cannot be used to do a traditional form submit like you are describing. So, to be able to post a value and navigate to the new page, we need to simulate a form post.

所以流程如下:


  1. 你点击图片,你的JS代码得到了 imgnum

  2. 接下来,有人点击 #see_comments

  3. 我们创建一个临时表单,其中 imgnum 值作为隐藏字段

  4. 我们提交该表单,其中发布值加载 comments.php

  5. 您的 comments.php 页面将有权访问已发布的变量(即在PHP中它将是 $ _ POST ['aid']

  1. You click on the image, and your JS code gets the imgnum
  2. Next, someone clicks on #see_comments
  3. We create a temporary form with the imgnum value in it as a hidden field
  4. We submit that form, which posts the value and loads the comments.php page
  5. Your comments.php page will have access to the posted variable (i.e. in PHP it would be $_POST['aid'])

这篇关于非AJAX jquery POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:26