jquery ajax中的JSON.stringify

时间:2017-10-08 11:24:43

标签: javascript jquery json ajax

我将document.write()用于json。在ajax之外的document.write(JSON.stringify({name:'jason',surname:'etc'}));工作,但在.done()中无效。我尝试删除dataType:'json'但无效。

$.ajax({
    url: "http://api.wunderground.com/api/24b969202160514e/geolookup/conditions/q/Turkey/zmw:00000.58.17352.json",
    dataType:'json'
})
.done(function(data) {
    console.log("success",data);
    document.write(JSON.stringify(data));
    //document.write(data);
})
.fail(function(data) {
    console.log("error",data);
})
.always(function() {
    console.log("complete");
});

2 个答案:

答案 0 :(得分:0)

如果您检查浏览器控制台,则会看到以下错误...

jquery-git.js:9648 Mixed Content: The page at 'https://jsfiddle.net/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://api.wunderground.com/api/24b969202160514e/geolookup/conditions/q/Turkey/zmw:00000.58.17352.json'. This request has been blocked; the content must be served over HTTPS.

将您的网址从http更改为https

https://api.wunderground.com/api/24b969202160514e/geolookup/conditions/q/Turkey/zmw:00000.58.17352.json

这应该有效。

Working Demo

答案 1 :(得分:0)

为什么不使用jQuery' getJSON

$.getJSON('https://api.wunderground.com/api/24b969202160514e/geolookup/conditions/q/Turkey/zmw:00000.58.17352.json')
.done(function(data) {
    console.log("success",data);
    document.write(JSON.stringify(data));
    //document.write(data);
})