ASP.NET MVC Controller - 500Error - asp.net

Why isn't my controller function called?
I always get 500error (in fiddler). I get no error in Visual Studio or an error site.
Controller:
[POST("/test1")] // attributerouting (works with GET methods)
public ActionResult test1(TreeViewItemModel aItem)
{
...
}
Client:
var tree = $("#demo2").jstree("get_json");
var c = JSON.stringify(tree);
$.ajax({
type: "POST",
url: "/test1",
data: tree,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
}
});

Some times 500 Internal Server Error occurred because of syntax error in the View of it's controller like { or } mismatch or etc. Did you check syntax of test1.cshtml?

problem was the data format:
solution:
public ActionResult test1(IEnumerable<TreeViewItemModel> aItem)
{
}
Client:
var tree = $("#demo2").jstree("get_json");
var c = JSON.stringify(tree);
$.ajax({
type: "POST",
url: "/test1",
data: c,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
}
});

Related

Ajax with asp.net aspx returns Undefined

I'm facing the following issue: Whenever I click on the button, the alert shows undefined.
Web method:
[WebMethod]
public static string getTest()
{
return "testing success";
}
Ajax script
<script type="text/javascript">
function getTest() {
$.ajax({
type: "POST",
url: "main.aspx/getTest",
data: "{}",
datatype: "json",
contenttype: "/application/json; charset=utf-8",
success: function (msg) {
alert(msg.d);
},
error: function (data) {
}
});
}
</script>
datatype should be dataType and contenttype should be contentType. Also remove / from start of "/application/json; charset=utf-8"
$.ajax({
type: "POST",
url: "main.aspx/getTest",
data: "{}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (msg) {
alert(msg.d);
},
error: function (data) {
}
});
Remove these from your ajax call
datatype: "json",
contenttype: "/application/json; charset=utf-8",
More about these
Differences between contentType and dataType in jQuery ajax function
What is content-type and datatype in an AJAX request?
You can find more details in the jQuery Ajax documentation

ASP.NET hello world AJAX post

I keep getting a 500 with the following C#/jQuery. Any given implementation may not be right and thats not a huge issue. I'm just trying to get a hello world up. It works if the c# has no arguments but as soon as I try to recieve data it gives the 500.
[WebMethod]
public static string Test(string s)
{
// never gets here
}
$.ajax({
type: "POST",
url: "ajax.aspx/" + method,
/*async: true,*/
data: "{data:'" + data + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
callback(data.d);
}
});
latest attempt is this which still doesnt work:
[WebMethod()]
public static string Test(string data)
{
// never gets here
return "hello world";
}
$.ajax({
type: "POST",
url: "ajax.aspx/Test",
data: "data: {data:'abc'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert("back");
}
});
I think you don't have to use MVC to make it work. I think the way you are passing json parameters are wrong. Please check below code and try and do let me know whether it works.
[WebMethod()]
public static string Test(string data)
{
// never gets here
return "hello world";
}
$.ajax({
type: "POST",
url: "ajax.aspx/Test",
data:'{"data":"abc"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
}
});
try this
[HttpPost]
public ActionResult Test(string x)
{
// never gets here
return Json(true)
}
$.ajax({
type: "Post",
url: "ajax/Test",
data: {x:'abc'},
dataType: "json",
success: function (data) {
alert("back");
}
});

Can Page Methods be used in .net 2.0 with Ajax extensions?

I need to call a server side method from JavaScript function in ASP.Net 2.0 framework. How can I do this?
Try This
function focuslost() {
mainForm.StartUpdating();
var pagePath = window.location.pathname;
$.ajax({
type: "POST",
url: pagePath + "/TextChanged",
data: ("{ 'pNTID':'" + $("#<%= txtNTID.txtClientId%>").val()) + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function(XMLHttpRequest, textStatus, errorThrown) {
mainForm.EndUpdating()
},
success:
function(result) {
if (result.d.length > 0) {
}
mainForm.EndUpdating()
}
});
}
[WebMethod]
public static string TextChanged(string pNTID)
{
retrun "";
}
Use WebMethod or AjaxMethod. Check Below
http://www.xdevsoftware.com/blog/post/Call-WebMethod-from-Javascript-in-ASPNET.aspx

How to read Model values and send them by ajax post

I have the following code and it`s pointing out errors as follows
Error 1 The name 'date' does not exist in the current context
Error 2 The name 'person' does not exist in the current context
What is wrong?
$("#test").Click(function () {
var date = $("#DateFrom").val();
var person = Model.SelectedPerson;
$.ajax({
url: '#Url.Action("testEmp","Employee",new {dateFrom = date, selectedPerson= person})',
type: 'GET',
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (result) {
$('#text).html(result);
},
});
return false;
});
Try this:
$.ajax({
url: '#Url.Action("ActionName")',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({ date: '..', person: '...' }),
success: function(result) {
}
});
EDIT: Take a look at this answer for the complete solution.
jquery ajax forms for ASP.NET MVC 3

asp.net page method with jquery and parameter

In my javascript, I have:
var testdate = "{'TheNewDate' : '12/02/2011'}";
$("#mydiv").click(function () {
$.ajax({
type: "POST",
url: "../Pages/Appointments.aspx/GetAppointements",
data: testdate,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: successFn,
error: errorFn
});
});
In my code behind I have
[WebMethod]
public static string GetAppointements(string DateInput)
{
var t = DateInput;
However, when I click to run the call, I get the error function to activate. When I change the code behind function to public static string GetAppointement() it works. But I guess my goal is to pass a parameter to the code behind. What am I missing?
Thanks.
Your parameter is called DateInput and not TheNewDate, so:
$('#mydiv').click(function () {
$.ajax({
type: 'POST',
url: '../Pages/Appointments.aspx/GetAppointements',
data: JSON.stringify({ dateInput: '12/02/2011' }),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: successFn,
error: errorFn
});
});
You should make your JSON data match the parameter name in the web service method.
var testdate = "{'DateInput' : '12/02/2011'}";

Resources