A few days ago to do a portal site, on the home page need to load meteorological data, the use of the Central Meteorological Bureau interface.
Just started to use JSONP in the foreground cross-domain request data, did not succeed ~
After replacing the request data in C # back ...
Front-End Code:
$ (function () { $.ajax ({ type: "GET", URL: "Service/getweather.ashx", dataType: "JSON", success : function (data) { var weatherms = '; Console.log (data); data = eval (data.weatherinfo); Weatherms + = ' City: ' + data.city+ ' weather conditions: ' +data.weather+ ' temperature: ' +data.temp2+ ' ~ ' +data.temp1; $ ("#innerWeather"). HTML (weatherms); } ); })
Background code:
Context. Response.ContentType = "Text/plain"; Get weather and parse HttpWebRequest request = (HttpWebRequest) httpwebrequest.create ("http://www.weather.com.cn/data/ Cityinfo/101110407.html "); Request. Timeout =; Request. Method = "GET"; HttpWebResponse response = (HttpWebResponse) request. GetResponse (); StreamReader sr = new StreamReader (response. GetResponseStream ()); String jsonstr = Sr. ReadLine (); Context. Response.Write (JSONSTR);
Input JSON directly to the foreground for processing.
Remember a little bit every day, convenient to use again later.
ASP. Get JSON weather data across domains