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.
我有一些问题从我的jquery ajax调用一个控制器方法。调用控制器方法并正确传入数据服务器名称。但是,在我的控制器可以向jquery返回任何内容之前,jquery进入错误状态。
Here is my jquery and controller code snippets:
这是我的jquery和控制器代码片段:
$.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.
在一切之上。达到错误时给出的结果值也没有用。
3 个解决方案
#1
1
Send your response back as JsonResult
将您的回复发送回JsonResult
[HttpPost]
public JsonResult serverLookup(string serverName)
{
string data = myMethod.getData();
return Json(data);
}
#2
1
Return a Json
:
返回一个Json:
return Json(new { result: data });
When you make an AJAX request to the controller, it needs a JsonResult
.
当您向控制器发出AJAX请求时,它需要一个JsonResult。
#3
0
I suppose that Your Content() return html. In that case You have to change dataType to html, Or change it according to your response.
我想你的Content()返回html。在这种情况下,您必须将dataType更改为html,或根据您的响应进行更改。
#1
1
Send your response back as JsonResult
将您的回复发送回JsonResult
[HttpPost]
public JsonResult serverLookup(string serverName)
{
string data = myMethod.getData();
return Json(data);
}
#2
1
Return a Json
:
返回一个Json:
return Json(new { result: data });
When you make an AJAX request to the controller, it needs a JsonResult
.
当您向控制器发出AJAX请求时,它需要一个JsonResult。
#3
0
I suppose that Your Content() return html. In that case You have to change dataType to html, Or change it according to your response.
我想你的Content()返回html。在这种情况下,您必须将dataType更改为html,或根据您的响应进行更改。