非AJAX jquery POST请求

编程入门 行业动态 更新时间:2024-10-09 17:25:03
本文介绍了非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 Neiner clarification: 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.

所以流程如下:

  • 你点击图片,你的JS代码得到了 imgnum
  • 接下来,有人点击 #see_comments
  • 我们创建一个临时表单,其中 imgnum 值作为隐藏字段
  • 我们提交该表单,其中发布值和加载 comments.php 页
  • 您的 comments.php 页面将有权访问已发布的变量(即在PHP中它将是 $ _ POST ['aid'] )
  • You click on the image, and your JS code gets the imgnum
  • Next, someone clicks on #see_comments
  • We create a temporary form with the imgnum value in it as a hidden field
  • We submit that form, which posts the value and loads the comments.php page
  • Your comments.php page will have access to the posted variable (i.e. in PHP it would be $_POST['aid'])
  • 更多推荐

    非AJAX jquery POST请求

    本文发布于:2023-10-10 22:03:25,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1479884.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:AJAX   jquery   POST

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!