简体   繁体   English

我无法将变量从JavaScript和jQuery传递给PHP,因此无法加载响应

[英]I can't passing variable to PHP from JavaScript and jQuery load the response

I want passing 2 parameters to PHP page via AJAX and load the response, but this code is not working. 我想通过AJAX将2个参数传递到PHP页面并加载响应,但是此代码不起作用。

JavaScript: JavaScript:

$(".show_category").click(function(){
    var category_id = $(this).attr('data-category');
    $.ajax({
        url: "conx.php",
        method: "POST",
        data: {
            action: "sort_category",
            category_id: category_id
        },
        success: function(data) {
            $("#con").load("conx.php");
        }
    });
});

PHP: PHP:

<?php
    echo "1".$_POST["action"]."<br/>";
?>

You issue is here: 您的问题在这里:

    success: function(data) {
        $("#con").load("conx.php");
    }

At this point, you have already posted the data and received the HTML response. 至此,您已经发布了数据并收到了HTML响应。 There is no need to make another HTTML request by calling .load , and doing so will mean requesting it again without the POST data, so it will not have the intended effect. 没有必要使通过调用另一个HTTML要求.load ,而这样做将意味着再次请求它没有POST数据,所以不会有预期的效果。 Just use the HTML you already have in the data argument. 只需使用data参数中已有的HTML。

    success: function(data) {
        $("#con").html(data);
    }

On a side note, this PHP code is a reflected XSS vulnerability : 附带说明,此PHP代码是一个反映的XSS漏洞

<?php
    echo "1".$_POST["action"]."<br/>";
?>

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

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