[英]Why response data from AJAX request comes in error and not success?
I have some JSON data stored at https://jsonblob.com that allows accessing JSON data through some unique url.我有一些 JSON 数据存储在https://jsonblob.com允许通过一些唯一的 url 访问 JSON 数据。
So below is my unique url.所以下面是我的唯一网址。
https://jsonblob.com/12a5fd37-03ee-11eb-909d-574de81f75ed https://jsonblob.com/12a5fd37-03ee-11eb-909d-574de81f75ed
JSON data in above url is:上面 url 中的 JSON 数据是:
{
"glossary": {
"title": "Suyog",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "12345",
"GlossDef": {
"para": "This is my data 123456",
"GlossSeeAlso": [
"GML",
"XML"
]
},
"GlossSee": "Foobar"
}
}
}
}
}
Now, I have used jQuery AJAX call to retrieve the JSON data as below.现在,我使用 jQuery AJAX 调用来检索 JSON 数据,如下所示。
$.ajax({
url: "https://jsonblob.com/cc73f020-03eb-11eb-909d-095d7b5c02dd",
type: 'GET',
dataType: 'json',
contentType: 'application/json',
processData: false,
success: function (data) {
console.log("Success: " + data.responseText);
},
error: function(data){
console.log("Error: "+data.responseText);
}
});
I have 3 main queries:我有3个主要查询:
$(document).ready(function() { $("#url").val("https://jsonblob.com/12a5fd37-03ee-11eb-909d-574de81f75ed"); }); $("#go").click(function() { $("#res1").html("<b>Please wait....Fetching data</b>"); $("#res2").text("\\n\\n\\nPlease wait....Fetching data"); $.ajax({ url: $("#url").val(), type: 'GET', dataType: 'json', contentType: 'application/json', processData: false, success: function(data) { console.log("Success: " + data.responseText); $("#res1").html("<b style='color:darkgreen;'>Success</b>\\n" + data.responseText); $("#res2").text("Success\\n" + data.responseText); }, error: function(data) { console.log("Error: " + data.responseText); $("#res1").html("<b style='color:red;'>Error</b>\\n" + data.responseText); $("#res2").text("Error\\n" + data.responseText); } }); }); $("#reset").click(function(){ $("#res1").html(""); $("#res2").text(""); });
body { text-align: center; padding: 5px; } #url { padding: 5px; margin-top: 20px; } #go,#reset { font-weight: bold; padding: 5px; border: 1px solid; border-radius: 4px; top: 20px; position: relative; cursor:pointer; background:grey; color:white; } #res1 { border: 1px solid; top: 20px; position: relative; height: 150px !important; max-height: 150px !important; overflow:scroll; padding: 5px; } #res2 { border: 1px solid; top: 20px; position: relative; height: 150px !important; max-height: 150px !important; overflow:scroll; padding: 5px; } span{ top:20px; position:relative; font-weight:bold; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="container"> <input id="url" type="text" size="70" /> <br /> <span id="go">Fetch data</span> <span id="reset">Reset data</span> <br /> <br /> <span>Response through AJAX(HTML)</span> <div id="res1"></div> <br /> <span>Response through AJAX(Text)<label style='background:yellow;'>This has my JSON data(Search "Suyog")</label></span> <div id="res2"></div> </div>
The url that you're requesting is sending HTML.您请求的 url 正在发送 HTML。 Because you specified
application/json
as the content type, getting HTML as the response would be an error.因为您将
application/json
指定为内容类型,所以获取 HTML 作为响应将是一个错误。
Try instead:试试吧:
$.ajax({
url: "https://jsonblob.com/api/cc73f020-03eb-11eb-909d-095d7b5c02dd",
type: 'GET',
dataType: 'json',
contentType: 'application/json',
processData: false,
success: function (data) {
console.log("Success: " + data.responseText);
},
error: function(data){
console.log("Error: "+data.responseText);
}
});
That sends a request to their REST api (note /api/
in the URL), which should return JSON.这会向他们的 REST api(注意 URL 中的
/api/
)发送一个请求,它应该返回 JSON。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.