0

Need some advice - doing a project with MVC 4 - used to Forms and Ajax.

Normally I do AJAX calls

My code looks like this :

$.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "@SiteConfig.BaseUrl/assessment/getquestion",
                data: "{'AssessmentId':" + "'" + AssessmentId + "'" + ",'PageNumber':" + PageIndex + "}",
                dataType: "json",
                beforeSend: function(){
                    $('#ajaxLoader').show();
                    $('#questionContainer').hide();
                },
                success: function (msg) {
                    var data = msg.d;
},
                complete: function(){
                    $('#ajaxLoader').hide();
                    $('#questionContainer').show();
                },
                error:function (request, status, error){
                    alert(request.responseText);
                    alert(request);

                    $('#ajaxLoader').hide();
                    //window.location = '@SiteConfig.BaseUrl/questionnaire';
                }   
            });

[HttpPost]
public JsonResult GetQuestion(Guid AssessmentId, Int32 PageNumber)
{
   ... my code
   return this.Json(assessmentInfo, JsonRequestBehavior.AllowGet);
}

I keep getting HTML that is returned instead of JSON - it basically just sends me the HTML for the entire page back in the method - what am I doing wrong?

Debugging the problem yields the following result from javascript: Javascript : SyntaxError: JSON.parse: enexpected character

Furthermore, if I add a breakpoint to the C# GetQuestion method, it is not being hit.

Could this be a URL routing issue?

Also, for the guys that are commenting on Naming conventions : the purpose of this method is to get a question. Naming conventions should follow business logic first, then technical conventions. Calling the method postquestion implies that I am posting a question which doesnt make sense at all.

4

2 回答 2

0

您应该像这样编辑您的 GetQuestion 方法:

[HttpPost]
public JsonResult GetQuestion(Guid AssessmentId, Int32 PageNumber)
{
   //my code
   return this.Json(myClass,JsonRequestBehavior.AllowGet);
}

还请正确命名约定,如果有什么,请调用方法 GET。如果您发布某些内容,请调用 POST 方法。

于 2012-11-26T14:38:06.380 回答
0

好的,我发现了问题。

确实是导致问题的路由问题。

于 2012-11-26T15:44:49.920 回答