Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ajax总是出现在代码的错误部分_C#_Asp.net Mvc_Jquery - Fatal编程技术网

C# ajax总是出现在代码的错误部分

C# ajax总是出现在代码的错误部分,c#,asp.net-mvc,jquery,C#,Asp.net Mvc,Jquery,我不太擅长ajax,但要使用我的一个jquery插件,我必须在ajax中进行一些调用。问题是它总是出现在函数的错误处理中,我不知道为什么。由于我使用的是VisualWebDeveloperExpress,javascript调试无法工作 以下是jquery函数: $('.auto-submit-star').rating({ callback: function (value, link) { $.ajax({

我不太擅长ajax,但要使用我的一个jquery插件,我必须在ajax中进行一些调用。问题是它总是出现在函数的错误处理中,我不知道为什么。由于我使用的是VisualWebDeveloperExpress,javascript调试无法工作

以下是jquery函数:

$('.auto-submit-star').rating({
            callback: function (value, link) {
                $.ajax({
                    type: "POST",
                    url: "/Recipe/Rate",
                    data: $("#rate").serialize(),
                    dataType: "text/plain",
                    success: function (response) {
                        if (response != 'false') {
                            alert('Your vote has been saved');
                        } else {
                            alert('You already voted for this recipe!');
                        }
                    },
                    error: function (response) {
                        alert('There is an error');
                    }
                });
            }
        });
然后,它进入控制器。我已经调试了这个部分,它工作了,它将好的值保存在数据库中,然后返回“false”或“”,这取决于用户是否已经投票

代码如下:

[HttpPost]
        [Authorize]
        public ActionResult Rate(FormCollection form)
        {
            var rate = Convert.ToInt32(form["Score"]);
            var id = Convert.ToInt32(form["IDRecipe"]);

            //On valide si l'utilisateur a déja voté
            Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey;
            var existingVote = db.StarRatings.Where(a => a.IDRecipe == id).Where(b => b.IDUser == userGuid).FirstOrDefault();

            if (existingVote != null)
                return Content("false");

            StarRating rating = new StarRating();
            rating.IDRecipe = id;
            rating.IDUser = (Guid)Membership.GetUser().ProviderUserKey;
            rating.Score = rate;

            var recipe = db.Recipes.Where(a => a.IDRecipe == id).First();
            recipe.StarRatings.Add(rating);
            db.SaveChanges();

            return Content("");
        }
任何人都可以告诉我为什么我总是收到消息“有一个错误”,而它从来没有出现在ajax调用的“成功”部分?我需要在控制器中返回一些特殊的东西吗

编辑

下面是internet explorer调试器中响应变量的屏幕截图,其中一条注释让我发现了该变量


谢谢

我建议进行以下更改:

[HttpPost]
[Authorize]
public JsonResult Rate(int Score, int IDRecipe)
{
    //On valide si l'utilisateur a déja voté
    Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey;
    var existingVote = db.StarRatings.Where(a => a.IDRecipe == IDRecipe).Where(b => b.IDUser == userGuid).FirstOrDefault();

    if (existingVote != null)
        return Json(false);

    StarRating rating = new StarRating();
    rating.IDRecipe = IDRecipe;
    rating.IDUser = (Guid)Membership.GetUser().ProviderUserKey;
    rating.Score = Score;

    var recipe = db.Recipes.Where(a => a.IDRecipe == IDRecipe).First();
    recipe.StarRatings.Add(Score);
    db.SaveChanges();

    return Json(true);
}
以及您的JavaScript调用:

$('.auto-submit-star').rating({
    callback: function (value, link) {
        $.ajax({
            type: "POST",
            url: "/Recipe/Rate",
            data: { IDRecipe: GetRecipeID(), Score: GetScore() },
            cache: false,
            success: function (response) {
                alert(response);
            },
            error: function (xhr, error) {
                alert(error);
            }
        });
    }
});

传递给
$.ajax
调用的
数据
对象值将作为一组查询参数附加到URL,从而生成一个类似于
/Recipe/Rate?IDRecipe=123&Score=123
的URL,该URL由MVC代码解释,以在调用
Rate
方法时找到要初始化的适当参数。类似地,
Rate
方法中的
Json(true)
Json(false)
返回值将转换为Json编码的字符串,并作为响应主体返回。

我能够通过使用
text
而不是
text/plain
来复制并修复它。我对原因的理解是,浏览器首先将响应解释为HTML,然后抛出一个错误。我可以在控制台中运行代码,并在遇到控制器返回的文本“Blah”并显示“意外令牌B”时看到解析错误

除了在Firefox中提到这个问题外,我找不到任何关于这个问题的可靠文档,但我使用的是Chrome,也有同样的问题。解决方法是使用
xhr.overrideMimeType(“text/plain;charset=x-user-defined”)
                $.ajax({
                    type: "POST",
                    url: "/Recipe/Rate",
                    data: $("#rate").serialize(),
                    dataType: "text",
                    success: function (response) {
                        if (response != 'false') {
                            alert('Your vote has been saved');
                        } else {
                            alert('You already voted for this recipe!');
                        }
                    },
                    error: function (response) {
                        alert('There is an error');
                    }
                });

您可以在浏览器中调试javascript,并且可以使用fiddler查看传输的内容。您是否可以打印错误消息?如果它在ajax调用中命中错误函数,则表示服务器端函数中发生错误。使用fiddler查看响应上的错误。当我发出警报(响应)时;我得到的不是警报(“有错误”),而是一个消息框,里面有:[object]@基思·尼古拉斯,我该怎么做?我设置了断点,但它没有停止,而且我已经读到MVC不支持调试。如何使用JSFIDLE调试本地计算机上的控制器???您使用的是什么浏览器?似乎很有趣,我将尝试一下GetRecipeID和GetScore从何而来?很高兴知道您可以重现该错误。我明天就试试