1,服务端:
public ActionResult GetJsonFiles()
{
Response.AddHeader("Access-Control-Allow-Origin", "*");
HttpContext.Response.ContentType = "application/json";
var callBackFunName = this.Request["callback"];
Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();
List<string> tempJsonDataFiles = new List<string>();
for (var i = 0; i < 24; i++)
{
tempJsonDataFiles.Add(gfsDataPath + "/" + DateTime.Parse(DateTime.Now.Date.ToShortDateString()).AddHours(i).ToString("yyyyMMddHH") + "00-temp-surface-level-gfs.json");
}
dic.Add("tempJsonDataList", tempJsonDataFiles);
var scriptContent = string.Format("{0}('{1}');", callBackFunName,JsonConvert.SerializeObject(dic));
//this.Response.Write(scriptContent);
return Json(JsonConvert.SerializeObject(dic), JsonRequestBehavior.AllowGet);
//return new EmptyResult();
}
2,前台JS端:
loadData: function () {
$.ajax({
type: "get",
url:"http://localhost:40876/MobileMap/GetJsonFiles?callback=?",
//url: ip+"/MobileMap/GetJsonFiles",
datatype: "jsonp",
jsonp:"callback",
crossDomain: true,
async: false,
success: function (json) {
if (json) {
var data=JSON.parse(json);
PageInfo.tempDataJsons = data.tempJsonDataList;
PageInfo.tempPics = data.tempPicList;
PageInfo.humiDataJsons = data.humiJsonDataList;
PageInfo.humiPics = data.humiPicList;
}
else {
//toastr.error("无法获取气象数据");
//alert("无法获取气象数据");
}
},
error: function (error) {
//toastr.error("无法获取气象数据");
//alert("获取气象数据失败");
}
});
//
// $.getJSON("http://localhost:40876/MobileMap/GetJsonFiles?callback=?",
// function(json) {
// alert(json.Name);
// alert(json.Email);
// }
// );
},
个人试验,AJAX设的jsonp属性没用,主要是 加了如下这句才解决的问题: Response.AddHeader("Access-Control-Allow-Origin", "*");