Reputation: 2655
I am having some issues calling a controller method from my jquery ajax. The controller method is called and the data servername
is passed in correctly. But, before my controller can return anything to the jquery, the jquery enters the error state.
Here is my jquery and controller code snippets:
$.ajax({
type: 'POST',
url: '@Url.Action("serverLookup", "QC")',
dataType: 'text',
data: { 'serverName': servername },
success: function (result) {
alert(result);
debugger;
},
error: function (result) {
debugger;
}
});
[HttpPost]
public ActionResult serverLookup(string serverName)
{
string data = myMethod.getData();
return Content(data);
}
On top of everything. The result value given when the error is reached is not helpful at all either.
Upvotes: 0
Views: 213
Reputation: 399
I suppose that Your Content() return html. In that case You have to change dataType to html, Or change it according to your response.
Upvotes: 0
Reputation: 5771
Send your response back as JsonResult
[HttpPost]
public JsonResult serverLookup(string serverName)
{
string data = myMethod.getData();
return Json(data);
}
Upvotes: 1
Reputation: 11330
Return a Json
:
return Json(new { result: data });
When you make an AJAX request to the controller, it needs a JsonResult
.
Upvotes: 1