This is my jquery code for ajax :
这是我对ajax的jquery代码:
$.ajax({
url: '@Url.Action("IsLoggedIn","Ajax")',
type: "POST",
data: "Command=CheckLogin",
cache:false,
success: function (dataFromServer) {
if ($.trim(dataFromServer) == "no") {
showLoginAndRegisterNowForm(obj);
}
},
error: function (a, b, c) {
alert(c);
}
});
This code wrote in separated .js file and add to .cshtml page with tag. My Controller name in Controllers
folder is AjaxController.cs
and it's different from this page Controller. Controller method is :
此代码在单独的.js文件中编写,并添加到带有标记的.cshtml页面。 Controllers文件夹中的我的控制器名称是AjaxController.cs,它与此页面控制器不同。控制器方法是:
[HttpPost]
public ActionResult IsLoggedIn(string Command)
{
return Content("yes");
}
When i use this code, ajax goes to error
function and said internal server error
. What should i do?
当我使用此代码时,ajax转到错误功能并说内部服务器错误。我该怎么办?
3 个解决方案
#1
0
Changing the Url,in separated js file to url: "/Ajax/IsLoggedIn"
solved the problem!
将分隔的js文件中的Url更改为url:“/ Ajax / IsLoggedIn”解决了问题!
#2
0
If you still want to programatically generate URLs based on your MVC routes, consider giving your script an initialization function where you pass in configuration, like URLs. Then initialize your script in your Razor view with an inline script block:
如果您仍希望以编程方式生成基于MVC路由的URL,请考虑为您的脚本提供一个初始化函数,您可以在其中传入配置,例如URL。然后使用内联脚本块在Razor视图中初始化您的脚本:
<script>
yourJSModule.initialize({
isLoggedInUrl: '@Url.Action("IsLoggedIn","Ajax")'
});
</script>
This way, you keep the benefit of non-hard-coded URLs and of separate script files (excepting your initialization).
这样,您可以保留非硬编码URL和单独脚本文件的优势(初始化除外)。
#3
0
I know you already solved the problem, but I sent you because maibe it is useful. Also it is usefull when you have set virtualpath. With this example, you don't need javascript in your page (cshtml).
我知道你已经解决了这个问题,但我寄给你的是因为maibe它很有用。当你设置了虚拟路径时,它也很有用。在这个例子中,你的页面中不需要javascript(cshtml)。
CSHTML:
<input type="hidden" id="UrlAjaxIsLoggedIn" value="@Url.Action("IsLoggedIn","Ajax")" />
JS:
$.ajax({
url: $('#UrlAjaxIsLoggedIn').val(),
type: "POST",
data: "Command=CheckLogin",
cache: false,
success: function (dataFromServer) {
if ($.trim(dataFromServer) == "no") {
showLoginAndRegisterNowForm(obj);
}
},
error: function (a, b, c) {
alert(c);
}
});
#1
0
Changing the Url,in separated js file to url: "/Ajax/IsLoggedIn"
solved the problem!
将分隔的js文件中的Url更改为url:“/ Ajax / IsLoggedIn”解决了问题!
#2
0
If you still want to programatically generate URLs based on your MVC routes, consider giving your script an initialization function where you pass in configuration, like URLs. Then initialize your script in your Razor view with an inline script block:
如果您仍希望以编程方式生成基于MVC路由的URL,请考虑为您的脚本提供一个初始化函数,您可以在其中传入配置,例如URL。然后使用内联脚本块在Razor视图中初始化您的脚本:
<script>
yourJSModule.initialize({
isLoggedInUrl: '@Url.Action("IsLoggedIn","Ajax")'
});
</script>
This way, you keep the benefit of non-hard-coded URLs and of separate script files (excepting your initialization).
这样,您可以保留非硬编码URL和单独脚本文件的优势(初始化除外)。
#3
0
I know you already solved the problem, but I sent you because maibe it is useful. Also it is usefull when you have set virtualpath. With this example, you don't need javascript in your page (cshtml).
我知道你已经解决了这个问题,但我寄给你的是因为maibe它很有用。当你设置了虚拟路径时,它也很有用。在这个例子中,你的页面中不需要javascript(cshtml)。
CSHTML:
<input type="hidden" id="UrlAjaxIsLoggedIn" value="@Url.Action("IsLoggedIn","Ajax")" />
JS:
$.ajax({
url: $('#UrlAjaxIsLoggedIn').val(),
type: "POST",
data: "Command=CheckLogin",
cache: false,
success: function (dataFromServer) {
if ($.trim(dataFromServer) == "no") {
showLoginAndRegisterNowForm(obj);
}
},
error: function (a, b, c) {
alert(c);
}
});