.NET MVC 4应用程序 - 从AJAX调用中的一个功能 [英] .net mvc 4 application - call from ajax to a function in controller
问题描述
我创建MVC 4应用程序,我用ajax从js文件调用一个函数在控制器中。
当我调用该函数从阿贾克斯,它的正确调用相应的功能。但无论是成功的还是错误的功能不点火。可能有人帮助我改正我的错误?
我想读取数据库将其转换为JSON格式,写它变成一个.js文件,然后成功的功能被解雇了的数据。帮我解决这个问题。先谢谢了。
下面是我的code。
$阿贾克斯({
//网址:'@ Url.Action(getJsonData,家),
网址:首页/ getJsonHugeData1
//数据:{},
键入:GET,
//的contentType:应用/ JSON,
//数据类型:JSON
成功:函数(){
警报();
警报('成功getJsonHugeData');
loaddata(数据);
},
错误:函数(){
警报('错误');
}
});
控制器:
公共JsonResult getJsonHugeData()
{
VAR用户= GetUsersHugeData();
JSON字符串=VAR数据源=;
JSON + = JsonConvert.SerializeObject(users.ToArray());
System.IO.File.WriteAllText(使用Server.Mappath(/脚本/ NewData.js),JSON);
返回JSON(用户,JsonRequestBehavior.AllowGet);
}
私人列表<&的usermodel GT; GetUsersHugeData()
{
VAR usersList =新的List<&的usermodel GT;();
用户的usermodel; 清单< dummyData>数据=新的List< dummyData>(); 使用(Database1Entities dataEntity =新Database1Entities())
{
数据= dataEntity.dummyDatas.ToList();
} 的for(int i = 0; I< data.Count;我++)
{
用户=新的usermodel
{
ID =数据[I] .ID,
产品名称=数据[I] .ProductName,
收入=数据[I] .Revenue,
InYear =数据[I] .InYear.Year
};
usersList.Add(用户);
}
}
我跑了所有code,除了下面的,因为你没有提供的usermodel和你的问题dummydata类:
私人列表<&的usermodel GT; GetUsersHugeData()
{
VAR usersList =新的List<&的usermodel GT;();
用户的usermodel; 清单< dummyData>数据=新的List< dummyData>(); 使用(Database1Entities dataEntity =新Database1Entities())
{
数据= dataEntity.dummyDatas.ToList();
} 的for(int i = 0; I< data.Count;我++)
{
用户=新的usermodel
{
ID =数据[I] .ID,
产品名称=数据[I] .ProductName,
收入=数据[I] .Revenue,
InYear =数据[I] .InYear.Year
};
usersList.Add(用户);
}
}
最终的结果是,你在你的阿贾克斯url参数,有一个错字。另外,如果你要检查错误,你的功能设置为
功能(jqxhr,状态,错误){
警报(错误);
}
检查抛出的错误。
I'm creating mvc 4 application where I call a function in controller from a js file using ajax.
When I call the function from ajax, its calling the respective function properly. But neither success nor error function is not firing . Could someone help me out to correct my mistake?
I would like to read the data from database convert it to json format and write it into a .js file and thereafter success function to be fired off. Help me to solve this. Thanks in advance.
Here is my Code.
$.ajax({
//url: '@Url.Action("getJsonData","Home")',
url: "Home/getJsonHugeData1",
//data: "{}",
type: "GET",
//contentType: 'application/json',
//dataType: "json",
success: function () {
alert();
alert('success getJsonHugeData');
loaddata(data);
},
error:function(){
alert('error');
}
});
Controller:
public JsonResult getJsonHugeData()
{
var users = GetUsersHugeData();
string json = "var dataSource=";
json += JsonConvert.SerializeObject(users.ToArray());
System.IO.File.WriteAllText(Server.MapPath("/Scripts/NewData.js"), json);
return Json(users, JsonRequestBehavior.AllowGet);
}
private List<UserModel> GetUsersHugeData()
{
var usersList = new List<UserModel>();
UserModel user;
List<dummyData> data = new List<dummyData>();
using (Database1Entities dataEntity = new Database1Entities())
{
data = dataEntity.dummyDatas.ToList();
}
for (int i = 0; i < data.Count; i++)
{
user = new UserModel
{
ID = data[i].Id,
ProductName = data[i].ProductName,
Revenue = data[i].Revenue,
InYear = data[i].InYear.Year
};
usersList.Add(user);
}
}
I ran all of your code except for the following since you didn't provide the UserModel and dummydata classes in your question:
private List<UserModel> GetUsersHugeData()
{
var usersList = new List<UserModel>();
UserModel user;
List<dummyData> data = new List<dummyData>();
using (Database1Entities dataEntity = new Database1Entities())
{
data = dataEntity.dummyDatas.ToList();
}
for (int i = 0; i < data.Count; i++)
{
user = new UserModel
{
ID = data[i].Id,
ProductName = data[i].ProductName,
Revenue = data[i].Revenue,
InYear = data[i].InYear.Year
};
usersList.Add(user);
}
}
The end result was that you had a typo in your ajax 'url' parameter. Also, if you are going to check for errors, set your function to
function(jqxhr, status, error) {
alert(error);
}
to check the error being thrown.
这篇关于.NET MVC 4应用程序 - 从AJAX调用中的一个功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!