code

AJAX가 아닌 jQuery POST 요청

codestyles 2020. 10. 18. 17:57
반응형

AJAX가 아닌 jQuery POST 요청


jQuery POST 함수를 사용하려고하는데 AJAX 스타일로 요청을 처리하고 있습니다. 내 말은 내가 가라고 말하고있는 페이지로 실제로 가지 않는다는 것을 의미합니다.

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

        }
    );
});

이 함수는 도움 값이있는 comments.php 페이지로 이동해야합니다 . 잘 게시되지만 comments.php로 리디렉션되지 않습니다 .


@Doug Neiner 설명 :

  1. 15 개의 링크 (이미지)가 있습니다. 링크를 클릭하면 JavaScript가로드됩니다. 스크립트는 imgnum내가 무엇 을 열 었는지 알고 있습니다. 이것은 comments.phpimgnum 에서 원합니다 . 이 자바 스크립트를 사용해야하고 다른 어떤 방법도 트릭을 할 수 없습니다. JavaScript는 필수입니다.

  2. 귀하의 방법은 지원 값을 성공적으로 게시합니다. 그러나 해당 값을 에코하려고 할 때 comments.php 에서 아무것도 표시하지 않습니다.

  3. Firebug를 사용하고 있습니다. 콘솔에는 (2) 단계에서 성공적으로 만든 에코 요청이 표시됩니다.


나는 당신이하려는 것이 무엇인지 알고 있지만 당신이 원하는 것은 아닙니다.

첫째, 서버에서 데이터를 변경 하지 않는 한 POST요청을 사용하지 마십시오 . 그냥 #see_comments평범 해져<a href='/comments.php?aid=1'>...

이 경우 사용하는 POST, 당신의 전화를 따르도록 페이지를 얻기 위해이 작업을 수행 :

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

이것이 실제로 어떻게 작동하는지.

첫 번째 $.postAJAX 방법 일 뿐이며form 설명하는 것처럼 전통적인 제출 을 수행하는 데 사용할 수 없습니다 . 그래서, 값 게시 할 수 있도록 하고 새로운 페이지로 이동을, 우리는 시뮬레이션 할 필요가 form게시물을.

따라서 흐름은 다음과 같습니다.

  1. 이미지를 클릭하면 JS 코드가 imgnum
  2. 다음으로 누군가가 #see_comments
  3. 숨겨진 필드로 값을 사용하여 임시 form만듭니다.imgnum
  4. 우리는 양식을 제출하는 게시물 값 로드 comments.php페이지를
  5. 귀하의 comments.php페이지에 게시 된 변수에 액세스 할 수 있습니다 (PHP에있을 것 즉 $_POST['aid'])

$("#see_comments").click(function () {
    $('<form action="comments.php" method="POST"/>')
        .append($('<input type="hidden" name="aid" value="' + imgnum + '">'))
        .appendTo($(document.body)) //it has to be added somewhere into the <body>
        .submit();
});

Doug Neiner의 솔루션은 정확할뿐만 아니라 가장 포괄적으로 설명 된 솔루션이지만 한 가지 큰 문제가 있습니다. Chrome에서만 작동하는 것 같습니다.

I fidgeted around for a while trying to determine a workaround, and then stumbled upon the second answer by nopnop77. The only difference is the extra code appendTo($(document.body)). Then I tested it in firefox and it worked like a charm. Apparently, Firefox and IE need to have the temporary form attached somewhere in the DOM Body.

I had to do this implementation for a Symfony2 project, since the path generator inside the .twig templates would only work with GET parameters and messing with the query string was breaking havoc with the security of the app. (BTW, if anyone knows a way to get .twig templates to call pages with POST parameters, please let me know in the comments).


i think what you're asking is to get to 'comments.php' and posting aid with value imgnum. The only way to do this is to submit this value with a form.

However, you can make this form hidden, and submit it on an arbitrary click somewhere with jquery.

html necessary (put anywhere on page):

<form id='see_comments_form' action='comments.php' action='POST'>
    <input id='see_comments_aid' type='hidden' name='aid' value=''>
</form>

js necessary:

$("#see_comments").click(function(){
    $('#see_comments_aid').val(imgnum);
    $('#see_comments_form').submit();
);

this will redirect to 'comments.php' and send the proper value imgnum (that i assume you are getting from somewhere else).


Actually, $.post() sends some data to the server. It does not cause any redirection unless you do it in your server side code which handles the POST request. I can suggest two solutions:

  1. To go to comment page, instead of using JQuery post, you can simply use a 'anchor' tag - <a href="comments.php?aid=1">Show Comments</a>.
  2. Or if you are want to go through JQuery, you can use this code snippet: $(location).attr("href", "comments.php?aid=1");

didnt exactly solve the problem. but did manage to work around it. i had to do a lot modification to the JS to make this work, but the core problem of this question was solved by doing this:

    $("#see_comments").attr({href: "comments.php?aid='"+imgnum+"'"});

this appended the aid value to the URL as @Doug Neiner initially suggested me to do. Thanks a lot Doug for all the effort. I really appreciate. +1 and accept to your answer for the effort.

참고URL : https://stackoverflow.com/questions/2054705/non-ajax-jquery-post-request

반응형