0

所以我真的迷失了这个,非常感谢一些帮助。

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

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

这是 JavaScript:

            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("");
        }

    }
4

1 回答 1

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);


            }
        }



    }
于 2013-06-07T16:33:59.310 回答