jQuery和AJAX。 POST仅提供NULL

时间:2016-04-19 13:27:42

标签: javascript php jquery html ajax

我在Cloud9中这样做。

我的计划是制作图片库。

在第一页:

  • 图片很少,代表不同的“类别”。
  • 点击其中一个,您将被移动到所选类别的下一页相册。
  • 通过点击相册,您将看到该相册中的图片。

在第一页上,我制作了几个类别。单击其中一个图片时,应激活以下代码。代码触发,jQuery移动到下一页。

问题是,AJAX发送了id,但它从未到达其他页面。发送的id是字符串,它包含正确的单词。

然而,捕获POST的另一个页面将不会收到它:变量的类型保持为NULL,并且它什么都不包含。

第一页的代码(mainpage.php):

        $(document).ready(function(){
            getContent("mainpage.php");
        });

        function getContent($sivu){
             var xhttp = new XMLHttpRequest();
             xhttp.onreadystatechange = function() {
                 if (xhttp.readyState == 4 && xhttp.status == 200) {
                     document.getElementById("kuvaa").innerHTML = xhttp.responseText;
                 }
             }
             xhttp.open("GET", $sivu, true);
             xhttp.send();
        }  


         $(document).on("click", ".picture", function(){

               var pictureId = $(this).attr("name");
               //window.alert(jQuery.type(pictureId)); //type is string
               //window.alert(pictureId); // content is what it should be

               var request = $.ajax({
                   url: 'folders.php',
                   type: 'POST',
                   data: {
                       'idd': pictureId
                   },

                   success : function() {
                      getContent("folders.php")  
                   },
                   error : function(err) {
                       // in case of error
                       console.log(err);
                       alert('error');
                    },
                    dataType: "html"
                });
            });

第二页上的变量(folders.php),它不会得到任何东西:

    $picId = $_POST["idd"];

第一页仍然正确读取folders.php(这意味着它成功发送POST,因为getContent必须成功运行)。

欢迎任何想法。

编辑,工作解决方案:

点击图片时:

            $(document).on("click", ".picture", function(){
                var pictureId = $(this).attr("name");
                getNewContent(pictureId);

            });

这就是这个:

            function getNewContent($idd){
                var xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function() {
                    if (xhttp.readyState == 4 && xhttp.status == 200) {
                        document.getElementById("kuvaa").innerHTML = xhttp.responseText;
                    }
                }
            xhttp.open("GET", "/folders.php?idd="+$idd, true);
            xhttp.send();
            } 

4 个答案:

答案 0 :(得分:1)

我建议你将参数从页面传递到其他整个查询字符串,如果你的页面不需要后端信息,你可以用Javascript捕获值,如果不是,你可以根据Hachachin'来获取值。答案。

/phpscript.php?idd=151

使用此脚本,您可以从查询字符串中获取idd

function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)", "i"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

var id = getParameterByName('idd');

答案 1 :(得分:0)

你使用了错误的dataType。

var settings = {
                    type: "POST",
                    dataType : "json"
                    beforeSend: function () {
                        self.enable(false);
                    }
                }; 

$.ajax(settings).done(function (data, textStatus, jqXHR){})

如果您需要将大对象传递到服务层以进行读取操作,则可能必须使用POST,因为GET具有大小限制。

答案 2 :(得分:0)

我猜你的页面php不能像那样工作,直到你在服务器上有它。 folders.php你应该把它移到wampserver。我想它会像C:\ Wamp \ www \ Folder \ folder.php 所以你的网址将是

url:'localhost/Folder/folder.php'

您需要抓住成功收到的数据。

success : function(data) {
     getContent(data);
},

我想你会有一些其他的错误

答案 3 :(得分:0)

使用xhttp.open("GET",/phpscript.php?id=151, true);发送id,并在php脚本中使用$ _GET。

相关问题