admin管理员组文章数量:1325961
Having trouble showing the JSON response using window.alert
or alert
in JavaScript. I'm not a native JS coder, I apologize for the noobness. Below are a few examples I've tried based on examples I found online.
My goal is to have the JSON response appear in an alert popup window.
# example 1
<script type="text/javascript">
var client = new HttpClient();
client.get('/', function(response) {
window.alert(response);
});
</script>
# example 2
$.get('/', function(responseText) {
alert(responseText);
});
# example 3
<script src=".3.1/jquery.min.js">
$.get('/', function(responseText) {
alert(responseText);
});
</script>
# example 4
var xhr = new XMLHttpRequest();
xhr.open('GET', "/", true);
xhr.send();
xhr.onreadystatechange = processRequest;
function processRequest(e) {
}
Having trouble showing the JSON response using window.alert
or alert
in JavaScript. I'm not a native JS coder, I apologize for the noobness. Below are a few examples I've tried based on examples I found online.
My goal is to have the JSON response appear in an alert popup window.
# example 1
<script type="text/javascript">
var client = new HttpClient();
client.get('https://website./json/', function(response) {
window.alert(response);
});
</script>
# example 2
$.get('https://website./json/', function(responseText) {
alert(responseText);
});
# example 3
<script src="https://ajax.googleapis./ajax/libs/jquery/3.3.1/jquery.min.js">
$.get('https://website./json/', function(responseText) {
alert(responseText);
});
</script>
# example 4
var xhr = new XMLHttpRequest();
xhr.open('GET', "https://website./json/", true);
xhr.send();
xhr.onreadystatechange = processRequest;
function processRequest(e) {
}
Share
Improve this question
asked Feb 24, 2018 at 20:33
AshleyAshley
291 gold badge1 silver badge2 bronze badges
2
-
Example two works for me using
https://stackoverflow.
. Check the console for errors. It is possible you're getting a cross origin request error for requesting https from http or something – Matthew Ciaramitaro Commented Feb 24, 2018 at 20:41 -
1
Assuming the cross-domain calls you're making are to domains which include CORS headers in the response (which is a little unlikely) then what you have should work. To help debug this I would suggest first checking the console for errors after making any requests (pressing F12 in your browser should open the dev console). Also, use
console.log()
instead ofalert()
. Alerting debug information is a bad idea as it coerces all data types to a string, so what you see may not be what you actually have. – Rory McCrossan Commented Feb 24, 2018 at 20:45
4 Answers
Reset to default 2The first problem that I can see is that the url https://website./json does not have a correct response.
you can for example try these links: https://jsonplaceholder.typicode./posts/1 or https://ipinfo.io/json
I prove your example 4 and it work perfectly.
var xhr = new XMLHttpRequest();
xhr.open('GET', "https://jsonplaceholder.typicode./posts/1", true);
xhr.send();
xhr.onreadystatechange = processRequest;
function processRequest(e) {
var response=JSON.stringify(e);
alert(response);
}
you can notice that I put the alert in the processRequest function and before display it. I used JSON.stringify to change the object e to a string. Let me knoe if that work for you.
When trying to display object in alert it will say [Object object]. Your response is a JSON. Stringify it, so that alert can display it.
$.get('https://website./json/', function(responseText) {
alert(JSON.stringify(responseText));
});
I have created a sample code on jsfiddle for your scenario:
$.ajax({
url: "https://jsonplaceholder.typicode./posts/1/ments",
type: "GET",
dataType: "json",
success: function(data){
alert(JSON.stringify(data));
}
});
https://jsfiddle/ashvinimaurya/kn3yv7Lh/7/
I'm making an ajax call and then alerting the response data after stringifying.
I think you are getting cors error, (cross origin error). You should set your request headers correctly according to the third party (https://website./json/) server headers.
本文标签: javascriptHow to show HTTP response in alert windowStack Overflow
版权声明:本文标题:javascript - How to show HTTP response in alert window? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742190269a2430081.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论