2013-04-27 44 views
2

當我的應用程序啓動這個JSonResult存在`噸,當我想用​​這個(我得到錯誤「類型錯誤」)對象存在,對象變化值JS,JQuery的

var newImg = $.parseJSON($("#UploadTarget").contents().find("#jsonResult")[0].innerHTML); 

我想什麼做?我想要告訴我這個對象存在的事件或這個對象改變的值。

var newImg = $.parseJSON($("#UploadTarget").contents().find("#jsonResult")[0].innerHTML); 

我該怎麼寫?

using System.Web.Mvc; 

    namespace pol 
    { 
     public class WrappedJsonResult : JsonResult 
     { 
      public override void ExecuteResult(ControllerContext context) 
      { 
       context.HttpContext.Response.Write("<html><body><textarea id=\"jsonResult\" name=\"jsonResult\">"); 
       base.ExecuteResult(context); 
       context.HttpContext.Response.Write("</textarea></body></html>"); 
       context.HttpContext.Response.ContentType = "text/html"; 
      } 
     } 
    } 

當我點擊按鈕圖片上傳到服務器,但結果不會opent重要方法UploadImage_Complete();

@model po.Models.Stwna 
@using (Html.BeginForm("UploadImage", "StronaGlowna", FormMethod.Post, 
             new 
              { 
               enctype = "multipart/form-data", 
               id = "ImgForm", 
               name = "ImgForm", 
               target = "UploadTarget" 
              })) 
{ 
    <input type="file" name="imageFile" /> 

    <input type="button" class="button" value="@Model.ZO" onclick="UploadImage()" /> 
} 
<iframe id="UploadTarget" name="UploadTarget" onload="UploadImage_Complete();" style="position: absolute; 
    left: -999em; top: -999em;"></iframe> 
<div id="Images"> 
</div> 
<script type="text/javascript"> 
    var isFirstLoad = true; 

    function UploadImage() { 
     $("#ImgForm").submit(); 
    } 
    function UploadImage_Complete() { 
     //Check to see if this is the first load of the iFrame 
     if (isFirstLoad == true) { 
      isFirstLoad = false; 
      return; 
     } 

     //Reset the image form so the file won't get uploaded again 
     document.getElementById("ImgForm").reset(); 

     //Grab the content of the textarea we named jsonResult . This shold be loaded into 
     //the hidden iFrame. 
     var newImg = $.parseJSON($("#UploadTarget").contents().find("#jsonResult")[0].innerHTML); 

     //If there was an error, display it to the user 
     if (newImg.IsValid == false) { 
      alert(newImg.Message); 
      return; 
     } 



     //Create a new image and insert it into the Images div. Just to be fancy, 
     //we're going to use a "FadeIn" effect from jQuery 
     var imgDiv = document.getElementById("Images"); 
     var img = new Image(); 
     img.src = newImg.ImagePath; 

     //Hide the image before adding to the DOM 
     $(img).hide(); 
     imgDiv.appendChild(img); 
     //Now fade the image in 
     $(img).fadeIn(500, null); 


     // $('#Images').text(newImg.ImagePath); 
    } 
</script> 

MVC

[HttpPost] 
     public WrappedJsonResult UploadImage(HttpPostedFileWrapper imageFile) 
     { 

      return new WrappedJsonResult 
      { 
       Data = new 
       { 
        IsValid = true, 
        Message = string.Empty, 
        ImagePath = Url.Content(String.Format("http://a1.ec-images.myspacecdn.com/images01/29/4982945eb42646efafe2f94855ac3d21/l.jpg")) 

       } 
      }; 
     } 

我得到當我點擊按鈕兩次,但是當我點擊一次,我不`噸得到任何結果,這一形象。

+0

什麼填充'#jsonResult'? – plalx 2013-04-27 21:49:33

+0

@plalx我添加到我的博客休息重要的東西 – 2013-04-27 21:53:20

+3

你等待,直到DOM準備好之前tyring操縱'textarea'?試試這個'$(function(){alert($。parseJSON($('#jsonResult')。val()));});' – plalx 2013-04-27 22:00:22

回答