Javascript—为什么我的JQuery AJAX请求失败?
我是JQuery新手,我真的不知道为什么这个get和post请求不起作用。我一直收到错误信息。我非常感谢您提供的任何建议或资源来帮助我解决这个问题。提前谢谢Javascript—为什么我的JQuery AJAX请求失败?,javascript,jquery,ajax,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,
$(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
});});
})
$(文档).ready(函数(){
$.getJSON(“http://api.github.com/users/noeladd,函数(json){$.ajax({
类型:“POST”,
url:“http://httpbin.org/post",
数据:json,
成功:函数(){
var parentDiv=document.getElementByClassName('container')[0];
var div=“
json.login
json.name”;
parentDiv.append(div);
} ,
错误:函数(){
警报(“请求失败!”)
},
数据类型:JSON
});});
})
我不明白。。。但是,把它改正过来
<!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>
样品
$(文档).ready(函数(){
$.getJSON(“http://api.github.com/users/noeladd,函数(json){
$.ajax({
类型:“POST”,
url:“http://httpbin.org/post",
数据:json,
成功:功能(数据){
//你没有使用数据…为什么???
var parentDiv=$('.container');
var div=“
”+json.login+”
“+json.name+”;
parentDiv.append(div);
} ,
错误:函数(){
警报(“请求失败!”)
},
数据类型:“json”
});});
})
我不知道你为什么要将json发布到另一个只返回发布的数据的网站上,但看看这个例子:
$(document).ready(function () {
$.getJSON("https://api.github.com/users/noeladd", function (json) {
$("<div />").append(
$("<img />").attr("src", json.avatar_url).css("width", 150),
$("<br />"),
$("<span />").text(json.login),
$("<br />"),
$("<span />").text(json.name)
).appendTo("div.container:eq(0)");
});
});
$(文档).ready(函数(){
$.getJSON(“https://api.github.com/users/noeladd,函数(json){
$(“”)。附加(
$(""),
$(“”)文本(json.login),
美元(“
”),
$(“”).文本(json.name)
).appendTo(“div.container:eq(0)”);
});
});
JSFIDLE上的工作示例:我是这样做的,因为问题的说明就是这样要求我做的。上面梅奥拉给我的答案帮助我找出了我做错了什么。非常感谢。这无疑帮助我找出了我做错了什么。