JQuery.ajax / asp.net:如果被调用的页面返回它可以工作的参数但是如果它执行linq查询则它失败

时间:2013-06-07 16:22:06

标签: jquery asp.net json

所以我真的迷失在这一点上,并会非常感谢一些帮助。

以下是我要做的事情:用户提交表单。下面的第二个javascript位将表单数据发送到一个页面,该页面将信息提交到数据库而不刷新页面。这工作正常,然后调用UpdateResponseText()。 UpdateResponseText调用一个页面来检索信息(下面的c#代码)并写出一些字符串。该值应出现在alert()框中。

这是问题所在:页面Article / GetResponse工作正常。我可以手动输入网址并获得预期的结果。但是,UpdateResponseText()的“成功”部分永远不会触发,因此我认为javascript存在问题。但奇怪的是,如果我不使用SQL而只是使用Response.Write参数,那么它可以工作。

这是Javscript:

            function UpdateResponseText() {
                alert("Function Called");
                $.ajax({
                    type: "GET",
                    dataType: "json",
                    url: "@string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"))/Article/GetResponse",
                data: "UserID=@Model.UserInfo.ID&ArticleID=@Model.Content.KEY",
                    success: function (json) {
                        alert("Success");
                    alert(json);
                }
            })
        }
            $('#ArticleFeedbackForm').submit(function () {
                var formdata = $(this).serialize();
                alert(formdata);
                $.ajax({
                    type: "POST",
                    url: "#",
                    data: formdata,
                    success: function () {
                        UpdateResponseText();
                    }
                });
                return false;
            });

C#:

    public void GetResponse(string UserID, string ArticleID)
    {

        HelpSiteEntities1 db = new HelpSiteEntities1();
        var ARID = Convert.ToInt32(ArticleID);
        FeedbackObject TheFeedbackObject = new FeedbackObject();
        var QuestionQuery = (from Q in db.Questions
                             join U in db.QuestionMaps
                             on Q.ID equals U.QuestionID
                             where U.TopicID == ARID
                             select new { TheQuestionText = Q.Question1, TheQuestionID = Q.ID }).ToList();
        var QuestionID = (QuestionQuery.Select(o => o.TheQuestionID).ToList()[0]);
        //Response.Write(UserID + ARID.ToString() + QuestionID);
        var AnswerOptionsQuery = from A in db.QuestionAnswers
                                 where (A.MapsToID == QuestionID)
                                 select new { TheAnswerText = A.AnswerText, TheAnswerIDs = A.ID, IsStatic = A.StaticResp };
        var AnswerOptions = AnswerOptionsQuery.ToList();
        var previousResponse = (from x in db.QuestionRespMaps
                                where x.UserID == UserID && x.QuestionID == 1 && x.TopicID == ARID
                                select x).FirstOrDefault();
        if (previousResponse != null)
        {

            Response.Write("You have indicated previously \"" + (AnswerOptionsQuery.Where(o => o.TheAnswerIDs == previousResponse.AnswerID).ToList())[0].TheAnswerText + "\". You may update your response below at any time.");
        }
        else
        {
           Response.Write("");
        }

    }

1 个答案:

答案 0 :(得分:1)

添加错误功能?

            error: function (jqXHR, textStatus, errorThrown) {
                HandleJQueryError(jqXHR, textStatus, errorThrown);
            }
        ,
            success: function (jsonResponse) {
                UpdateResponseText(jsonResponse);
            }









    function HandleJQueryError(jqXHR, textStatus, errorThrown) {

        try {

            var msg = "";

            if (null != jqXHR) {

                if (null != jqXHR.responseText) {
                    msg += "\n\r " + jqXHR.responseText;
                }
                if (null != jqXHR.responseXML) {
                    msg += "\n\r " + jqXHR.responseXML;
                }

                try {
                    if (null != jqXHR.statusText) {
                        msg += "\n\r " + jqXHR.statusText;
                    }
                }
                catch (err) {
                    msg += "\n\r " + err;
                }

            }

            if (null != textStatus) {
                msg += "\n\r " + textStatus;
            }

            if (null != errorThrown) {
                msg += "\n\r " + errorThrown;
            }

            alert(msg);



        }
        catch (err) {
            //alert("HandleJQueryError Error");
            if (null != err) {
              alert(err);


            }
        }



    }