Javascript-为什么我的JQuery AJAX请求失败?

时间:2016-05-01 20:03:48

标签: javascript jquery ajax

我是JQuery的新手,我真的不确定为什么这个get和post请求不起作用。我一直收到错误消息。我很感激任何建议或资源来帮助解决这个问题。提前谢谢。

 $(document).ready(function(){
  $.getJSON("http://api.github.com/users/noeladd", function(json){$.ajax({
  type: "POST",
  url: "http://httpbin.org/post",
  data: json,
  success: function(){ 
    var parentDiv = document.getElementByClassName('container')[0];
    var div = "<div><img src = 'json.avatar_url' width = '150px'></img><br> json.login <br> json.name</div>";
    parentDiv.append(div);
  } ,
  error: function(){
    alert("request failed!")
  },
  dataType: JSON
});});
})

2 个答案:

答案 0 :(得分:0)

我不明白......但要纠正。

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Sample</title>
  <link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <div class="container">

  </div>
  <script src="js/jquery/jquery-2.1.1.min.js"></script>
  <script>
  $(document).ready(function(){
    $.getJSON("http://api.github.com/users/noeladd", function(json){
      $.ajax({
        type: "POST",
        url: "http://httpbin.org/post",
        data: json,
        success: function(data){
          //You not using data... why???  
          var parentDiv = $('.container');
          var div = "<div><img src = '"+json.avatar_url+"' width = '150px'></img><br> "+json.login+" <br> "+json.name+"</div>";
          parentDiv.append(div);

        } ,
        error: function(){
          alert("request failed!")
        },
        dataType: 'json'
      });});
    })
    </script>
  </body>
  </html>

答案 1 :(得分:0)

我不知道为什么你把json发布到另一个只发回dada的网站,但看看这个例子:

:- op(1105, xfy, '|').

JSFiddle上的工作示例:https://jsfiddle.net/evandroprogram/Lbqbewsz/

相关问题