json和Jsonp 使用总结(2)
1.Jsonp的使用
var phoneAgent = navigator.userAgent; var urlDomaintest = " ";
function getHref() { var href = window.location.href; return href; } var PhoneAgent=“”;
$(function () {
$.ajax
({
async: false,
url: urlDomaintest + "Home/CloudUserInfo",
type: 'GET',
data: { UserName: phoneAgent, UserEmail: getHref() },
dataType: "jsonp", jsonp: "callback",
success: function (data) {
console.log("success");
if (data.Status==1) { var docSpan = document.getElementById("NumSpan"); docSpan.innerHTML = data.ResultNum; }
else { var docSpan = document.getElementById("NumSpan"); docSpan.innerHTML = "3500"; }
}
}) });
//对应接口
public ActionResult CloudUserInfo(string callback, string UserName, string UserEmail)
{
AjaxResult<string> jsonResult = new AjaxResult<string> { Status = -1, ResultNum = 0, Data = "" };
//return Json(jsonResult, JsonRequestBehavior.AllowGet);
return JavaScript(string.Format("{0}({1});", callback, Newtonsoft.Json.JsonConvert.SerializeObject(jsonResult)));
}
3. jonp 处理
$.ajax({
async: false,
url: url + "/Collect/StInfoData",
type: 'GET',
data: { 'userData': JSON.stringify(data) },
dataType: "jsonp",
jsonp: "jsonpcallback",
success: function (data)
{
alert("324345");
console.log(data);
var st = eval(data);
if (data.Status > 0) {
alert("提交成功");
}
else { alert("提交失败"); }
}
})
public ActionResult StInfoData(string userData,string jsonpcallback)
{
AjaxResult jsonResult = new AjaxResult { Status = -1, Message = "保存信息出错" };
return JavaScript(string.Format("{0}({1});", jsonpcallback, Newtonsoft.Json.JsonConvert.SerializeObject(jsonResult)));
}
3.代码处理
jsonp 无法异步大量数据