dolphone bubleine
dolphone bubleine

Reputation: 691

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");
    }
});

Upvotes: 1

Views: 2795

Answers (2)

Nilesh Thakkar
Nilesh Thakkar

Reputation: 2895

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);
    }
});

Upvotes: 1

Mohamed Farrag
Mohamed Farrag

Reputation: 1692

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");
    }
});

Upvotes: 0

Related Questions