ajax请求将变量传递给另一个页面

时间:2016-03-31 08:02:05

标签: javascript ajax

我有以下错误,我不明白为什么我的" div"中没有显示任何内容。 我想在我的div" mycontent"中的一个名为ticket.php的新页面中显示我的票证内容。

script.js:

function displaytickets(){

  var newid = {};


    $("#mylist").empty();
    $("#nbtick").html("");
    $("#mycontent").html("");
    var y = document.getElementById("mySecond").value;

    $.ajax({
      url: "https://cubber.zendesk.com/api/v2/users/"+y+"/tickets/requested.json",
      type: 'GET',
      dataType: 'json',
      cors: true ,
            contentType:'application/json',
            secure: true,
            beforeSend: function (xhr) {
                xhr.setRequestHeader ("Authorization", "Basic " + btoa(""));
            },
            success: function (data){
                for (i = 0; i < data.tickets.length; i++) {
                    var mytitle = data.tickets[i].subject;
          var description = data.tickets[i].description;
          var status = data.tickets[i].status;
          var myid = data.tickets[i].id;
          console.log(data.tickets[i]);
          var created = data.tickets[i].created_at;


          var nbticket = data.tickets.length;



                    $("#mylist").append('<li id="newlist" value="'+myid+'" onclick="ticketcontent('+myid+')">'+ mytitle +" #"+ myid+ "---"+status +"---"+created+'</li>')

                }
        $("#nbtick").append('<p>'+nbticket+'</p>');
            },


  });
  $("#idisplay").css("display", "none");


}






    function ticketcontent (newid){
      window.location = "http://localhost:8888/support-cubber/ticket.php";
      // var newid = {};
      // var newid = document.getElementById("newlist").value;
      console.log(newid);
      $.ajax({
          url: "https://cubber.zendesk.com/api/v2/tickets/"+newid+"/comments.json",
          type: 'GET',
          cors: true,
          dataType: 'json',
          contentType:'application/json',
          secure: true,
          beforeSend: function (xhr) {
              xhr.setRequestHeader ("Authorization", "Basic " + btoa(""));
          },
          success: function (data){


            for (var i = 0; i < data.comments.length; i++) {
            var alldata = data.comments[i].body
            $("#mycontent").append('<p>'+alldata+'</p>');

            console.log(alldata);
            }

          },
      });
      var newid = {};
    }

ticket.php:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Support Cubber</title>
  </head>
  <body>
    <h1>Hello world !</h1>
    <div id="mycontent">
<p>
</p>
    </div>
  </body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="script.js" type="text/javascript"> </script>


</html>

1 个答案:

答案 0 :(得分:0)

<#>“#Guillaume#Nouhaud”,您需要在某些事件上执行“ticketcontent”功能,例如body onload或表单提交或按钮点击。无论你在哪里写的。 例如:

<script type='text/javascript'>
jQuery(document).ready(function(){
    jQuery("button").click(function(){
        jQuery.ajax({url: "demoUrl", success: function(result){
            jQuery("#div").html('ajax worked!');
        }});
    });
});
</script>