如果想以MVC接口返回jsonp格式或者json格式给前台的Android调用,那么就要看以下的简单例子就明白了什么是JSONP。
MVC后台数据设置:
MVC控制器方法
public ActionResult GetPerson()
{
Employee e = new Employee();
e.Age = 20;
e.Company = "华为";
e.Conference = "年终大会";
e.name= "Janekim";
return GetJsonP(e, callback);//调用GetJson方法,传参数
}
//GetJson方法
public ActionResult GetJson(object obj,string callback)
{
if (string.IsNullOrEmpty(callback))
Return JavaScript(String.Formate({0}({1}),callback,obj));//如果callback不为空,那么返回jsonp格式给前台
Return new JsonpResult(obj,callback);//如果callback为空,那么返回json格式给前台
}
前台请求:
//xxx/JsonpDemo/GetPerson?id=11
返回这样的结果:
{"Age":20,“Company”:"华为",Conference":"年终大会","Name":"Janekim"}返回json格式给前台
前台请求:
//xxx/JsonpDemo/GetPerson?id=11&callback=Hello
返回这样的结果
Hello({"Age":20,“Company”:"华为",Conference":"年终大会","Name":"Janekim"}) 返回jsonp格式给前台
如果有描述不恰当,请指教,希望这个Demo对你很大帮助。
如果想要详细的Demo,请看另外一个实现jsonp的案例: http://www.cnblogs.com/janekimyan/