2014-10-28 86 views
0

我有控制器返回PartialView。此外我有JavaScript,通過結果捕獲此返回來做一些事情。但這是問題。獲取部分視圖在Ajax中使用jquery渲染零件

使這個結果顯示文本(result.responseText),我得到了奇怪的HTML代碼。

<$A$> <div</$A$><$B$> class="container"</$B$><$C$> id="regi"</$C$><$D$>> </$D$><$E$> </$E$><$F$><input name="__RequestVerificationToken" type="hidden" value="VKMPnUu9UA3ikkIA9o6mBPks6TNIpbNkwUtulJiWP-YOuKyYcXxyCknqCf_YAvyj1BGVZJHRBqLtjIttcFFENjgbx_RrttQZcgUJh8Lxb5w1" /></$F$><$G$> <h4>Create a new account.aassss</h4> <hr /> </$G$><$H$> </$H$><$I$><div class="validation-summary-errors" data-valmsg-summary="true"><ul><li>The Password must be at least 6 characters long.</li> </ul></div></$I$><$J$> <div</$J$><$K$> class="form-group"</$K$><$L$>> </$L$><$M$> </$M$><$N$><label class="col-md-2 control-label" for="UserName">User name</label></$N$><$O$> <div</$O$><$P$> class="col-md-10"</$P$><$Q$>> </$Q$><$R$> </$R$><$S$><input class="form-control" data-val="true" data-val-required="User name 필드가 필요합니다." id="UserName" name="UserName" type="text" value="asd" /></$S$><$T$> </div> </div> <div</$T$><$U$> class="form-group"</$U$><$V$>> </$V$><$W$> </$W$><$X$><label class="col-md-2 control-label" for="Password">Password</label></$X$><$Y$> <div</$Y$><$Z$> class="col-md-10"</$Z$><$a$>> </$a$><$b$> </$b$><$c$><input class="input-validation-error form-control" data-val="true" data-val-length="The Password must be at least 6 characters long." data-val-length-max="100" data-val-length-min="6" data-val-required="Password 필드가 필요합니다." id="Password" name="Password" type="password" /></$c$><$d$> </div> </div> <div</$d$><$e$> class="form-group"</$e$><$f$>> </$f$><$g$> </$g$><$h$><label class="col-md-2 control-label" for="ConfirmPassword">Confirm password</label></$h$><$i$> <div</$i$><$j$> class="col-md-10"</$j$><$k$>> </$k$><$l$> </$l$><$m$><input class="form-control" data-val="true" data-val-equalto="&#39;Confirm password&#39;과(와) &#39;Password&#39;이(가) 일치하지 않습니다." data-val-equalto-other="*.Password" id="ConfirmPassword" name="ConfirmPassword" type="password" /></$m$><$n$> </div> </div> </div> </$n$><$o$> </$o$><$p$> </$p$><$q$> </$q$><$$$><map><file path="~/Views/Shared/_Register.cshtml" encoding="ks_c_5601-1987"><node id="A" start="46" length="10" literal="true" /><node id="B" start="56" length="18" literal="true" /><node id="C" start="74" length="10" literal="true" /><node id="D" start="84" length="3" literal="true" /><node id="E" start="87" length="8" literal="true" /><node id="F" start="96" length="23" /><node id="G" start="119" length="64" literal="true" /><node id="H" start="183" length="8" literal="true" /><node id="I" start="192" length="24" /><node id="J" start="216" length="14" literal="true" /><node id="K" start="230" length="19" literal="true" /><node id="L" start="249" length="3" literal="true" /><node id="M" start="252" length="12" literal="true" /><node id="N" start="265" length="73" /><node id="O" start="338" length="18" literal="true" /><node id="P" start="356" length="18" literal="true" /><node id="Q" start="374" length="3" literal="true" /><node id="R" start="377" length="16" literal="true" /><node id="S" start="394" length="65" /><node id="T" start="459" length="50" literal="true" /><node id="U" start="509" length="19" literal="true" /><node id="V" start="528" length="3" literal="true" /><node id="W" start="531" length="12" literal="true" /><node id="X" start="544" length="73" /><node id="Y" start="617" length="18" literal="true" /><node id="Z" start="635" length="18" literal="true" /><node id="a" start="653" length="3" literal="true" /><node id="b" start="656" length="16" literal="true" /><node id="c" start="673" length="66" /><node id="d" start="739" length="50" literal="true" /><node id="e" start="789" length="19" literal="true" /><node id="f" start="808" length="3" literal="true" /><node id="g" start="811" length="12" literal="true" /><node id="h" start="824" length="80" /><node id="i" start="904" length="18" literal="true" /><node id="j" start="922" length="18" literal="true" /><node id="k" start="940" length="3" literal="true" /><node id="l" start="943" length="16" literal="true" /><node id="m" start="960" length="73" /><node id="n" start="1033" length="60" literal="true" /><node id="o" start="2128" length="4" literal="true" /><node id="p" start="6409" length="10" literal="true" /><node id="q" start="7537" length="2" literal="true" /></file></map> 

請任何人都可以幫助我做到這一點,使真正的HTML?或者在這段代碼中做錯了什麼?

$("#Register").click(function (e) { 
     var str = $("form").serialize(); 
     //var model = form.serialize(); 
     //$("#test").text(str); 
     $.ajax({ 
      url: RegisterURL.RegisterUrl, 
      type: 'POST', 
      cache: 'false', 
      data: str, 
      dataType: "json", 
      sucess: function (result) { 
       alert("asdad"); 
       //$("#myModal").html(result); 
      }, 
      error: function (result, status) { 
       alert("Fails"); 
       $("#test").text(result.responseText); 
       $("#Content").html(result.responseText); 
       var urlAct = '@Url.Action("Register","Account")' 
       alert(result); 
      } 
     }) 
    }) 
</script> 

這裏是

[HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> Register(RegisterViewModel model) 
     { 

      if (ModelState.IsValid) 
      { 
       var user = new ApplicationUser() { UserName = model.UserName }; 
       var result = await UserManager.CreateAsync(user, model.Password); 
       if (result.Succeeded) 
       { 
        await SignInAsync(user, isPersistent: false); 

         return Json("Success"); 

        //return Json 
        //return RedirectToAction("index", "Home"); 
       } 
       else 
       { 
        //return RedirectToAction("Index", "Home"); 
        AddErrors(result); 
        //if (Request.IsAjaxRequest()) 
        //{ 
        // return Json(new { message = "Fail" }); 
        //} 
       } 
      } 

      // If we got this far, something failed, redisplay form 
      return PartialView("_Register",model); 
     } 
+0

我認爲這是一個錯字,但你'sucess',而不是'success'爲Ajax處理程序。 – 2014-10-29 14:25:48

回答

0

使用dataType: "text"而不是dataType: json從服務器期望的結果是HTML,而不是JSON。

您的服務器代碼實際上會返回一個簡單的JSON字符串「success」或HTML,因此您還需要不返回成功案例的JSON結果(僅返回純文本)。

您還可以在評論中提及了一個錯字:

sucess =>success