Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
486 views
in Technique[技术] by (71.8m points)

jsonp返回数据有中文,不是乱码,但是报错 SyntaxError

代码
function getWeather(){
    var url = "http://www.weather.com.cn/data/cityinfo/101190408.html?callback=weather()";
    var script = document.createElement('script');
        script.setAttribute('src', url);
        document.getElementsByTagName('head')[0].appendChild(script); 
    
}
function weather(data){
    console.log(data);
}
报错信息
Uncaught SyntaxError: Unexpected token : 101190408.html?callback=weather():1 
获得数据
{"weatherinfo":{"city":"太仓","cityid":"101190408","temp1":"2℃","temp2":"16℃","weather":"晴转多云","img1":"n0.gif","img2":"d1.gif","ptime":"18:00"}}

谢谢!

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

callback的weather不要加括号

貌似此接口不支持 jsonp


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...