So having played around for the afternoon I've finally figured out the problem. It appears that if I place the above code into a its own JS file it doesn't work but if I include the script in the html it works. Any ideas? I'd rather have it in its own js
file
I follow this case since the beginning hoping can solve my problem also. I tried all the reply, and last I have my own problem.
I know the problem from "window.alert('error:
' + error);", it is "Internal Server Error" or HTTP Error 500. It
seem I can not generate in serialized JSON format. I can see this when I try "localhost:xxxxx/api/values" it is using xml format. But I am not really sure.
My Question, how do you serialize JSON format on your ApiController in your project? Could you please post your ApiController to help me to solved my problem? I am new in the web application world.
Here my controller When in debug mode, I can see the data from my sql server var "all":
// GET api/PersonCategory
public IEnumerable<PersonCategory> GetPersonCategories()
{
var all = repository.GetAll().ToList();
return all.AsEnumerable();
//return repository.GetAll().ToList().AsEnumerable();
}
Based on my experience to day, I found that when I add script on my .html it did not work. And I try to put all .css and .js on BundleConfig.cs at App_Start folder, and all .css and .js files are ok.
You may put your .js on the BundleConfig.cs file. My asumption your are using MVC 4 RC version.
ruddj16
Member
59 Points
92 Posts
Re: calling controller method using ajax
Jun 25, 2012 05:20 PM|LINK
So having played around for the afternoon I've finally figured out the problem. It appears that if I place the above code into a its own JS file it doesn't work but if I include the script in the html it works. Any ideas? I'd rather have it in its own js file
jsiahaan
Contributor
2326 Points
598 Posts
Re: calling controller method using ajax
Jun 26, 2012 01:13 AM|LINK
Hi ruddj16,
I follow this case since the beginning hoping can solve my problem also. I tried all the reply, and last I have my own problem.
I know the problem from "window.alert('error: ' + error);", it is "Internal Server Error" or HTTP Error 500. It seem I can not generate in serialized JSON format. I can see this when I try "localhost:xxxxx/api/values" it is using xml format. But I am not really sure.
My Question, how do you serialize JSON format on your ApiController in your project? Could you please post your ApiController to help me to solved my problem? I am new in the web application world.
Here my javascript inside my cshtml:
$(document).ready(function () { var API_URL = "api/PersonCategory/"; //function PersonCategoryViewModel() { // var self = this; // alert('Theseeeeeeeeee'); // $.getJSON(API_URL, self.personCategories).complete(function (data) { // alert('error' + error); // }); //}; //ko.applyBindings(new PersonCategoryViewModel()); $.ajax({ type: "GET", url: API_URL, datatype: "JSON", contentType: "application/json; charset=utf-8", success: function (returndata) { if (returndata.ok) { //do something with returndata.mydat window.alert(' everything is okay : ' + returndata.data.categoryName); } else { window.alert(' everything is okay : ' + returndata.data.categoryName); } }, error: function (xhr, desc, error) { //window.alert(xhr); //window.alert('description: ' + desc); window.alert('error: ' + error); } }); });Here my controller When in debug mode, I can see the data from my sql server var "all":
// GET api/PersonCategory public IEnumerable<PersonCategory> GetPersonCategories() { var all = repository.GetAll().ToList(); return all.AsEnumerable(); //return repository.GetAll().ToList().AsEnumerable(); }Hope you can help me,
Thanks.
Indonesian Humanitarian Foundation
jsiahaan
Contributor
2326 Points
598 Posts
Re: calling controller method using ajax
Jun 26, 2012 04:30 AM|LINK
Hi ruddj16,
Based on my experience to day, I found that when I add script on my .html it did not work. And I try to put all .css and .js on BundleConfig.cs at App_Start folder, and all .css and .js files are ok.
You may put your .js on the BundleConfig.cs file. My asumption your are using MVC 4 RC version.
I am using VS2012 RC on Win 8 RP.
This may can help you.
Indonesian Humanitarian Foundation