【问题标题】:Always error funtion is executing in json ajax in asp.net controller总是错误函数在 asp.net 控制器的 json ajax 中执行
【发布时间】:2016-06-04 08:54:16
【问题描述】:

Java 脚本:-

function themechange(themeValue) {

    $('#themeId').attr('href', '/zcv/resources/css' + '/' + 'theme-' + themeValue.toLowerCase() + '.css');
    $.ajax({
        url: '@Url.Action("ChangeTheme", "Login")',
        type: 'GET',
        dataType: 'json',
        cache: false,
        data: { 'themeValue': themeValue },
        success: function (data) {
           alert(data);
        },
        error: function () {
           alert('Error occured');
        }
   });
}

在 LoginContoller.cs 中:-

[HttpGet]
public JsonResult ChangeTheme(string themeValue)
{
    System.Diagnostics.Debug.WriteLine("======ChangeTheme======");

    Session["theme"] = themeValue;
    String x= "========";
    return Json(x, JsonRequestBehavior.AllowGet);
}

到目前为止回复的所有建议都不起作用。请提供 ASP.NET MVC 应用程序的可执行项目代码链接,该应用程序将简单的 ajax 与 json 应用于仅调用控制器方法。

【问题讨论】:

  • 你确定控制器没有异常
  • ajax 请求是否进入控制器操作?
  • 没有错误。 ajax 请求不会进入控制器操作
  • url: '/Login/ChangeTheme',现在试过了,但是ajax中没有进度错误函数正在加载
  • @Scripts.Render("~/bundles/jquery") 添加到视图页面解决了问题

标签: c# jquery asp.net json ajax


【解决方案1】:

您忘记通过 $('#themeId') 分配从 HTML 页面获取的 themeValue 并且您使用 GET 而不是 POST。

试试这个

function themechange(themeValue) {                    
    var themeValue = $('#themeId').attr('href', '/zcv/resources/css' + '/' + 'theme-' + themeValue.toLowerCase() + '.css');
    $.ajax({
         url: '@Url.Action("ChangeTheme", "Login")',
         type: 'POST',
         dataType: 'json',
         cache: false,
         data: { 'themeValue': themeValue },
         success: function (data) {
                alert(data);
         },
         error: function () {
               alert('Error occured');
         }
    });
}


[HttpPost]
public JsonResult ChangeTheme(string themeValue)
{
    System.Diagnostics.Debug.WriteLine("======ChangeTheme======");

    Session["theme"] = themeValue;
    String x= "========";
    return Json(x, JsonRequestBehavior.AllowGet);
}

【讨论】:

    猜你喜欢
    • 2019-01-13
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多