Javascript 对象存在,对象更改值JS,JQuery
当我的应用程序启动时,这个JSonResult不存在,当我想使用它时(我得到错误“TypeError”) 我想做什么?我想有一个事件,它会告诉我这个对象存在或这个对象改变值Javascript 对象存在,对象更改值JS,JQuery,javascript,jquery,Javascript,Jquery,当我的应用程序启动时,这个JSonResult不存在,当我想使用它时(我得到错误“TypeError”) 我想做什么?我想有一个事件,它会告诉我这个对象存在或这个对象改变值 var newImg = $.parseJSON($("#UploadTarget").contents().find("#jsonResult")[0].innerHTML); 我该怎么写 using System.Web.Mvc; namespace pol { public cla
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";
}
}
}
当我点击两次按钮时,我得到了这个图像,但当我点击一次按钮时,我没有得到任何结果。我决定使用定时器来制作这张图像。什么填充了
#jsonResult
?@plalx我添加到我的post rest重要内容你是否等到DOM准备好后才开始操作文本区域?试试这个$(函数(){alert($.parseJSON($('jsonResult').val());})代码>@plalx我编辑了这篇文章。这段代码的工作很好,如果你不使形式成为弹出窗口,但我想这样做的弹出窗口。我必须单击两次才能在网站上获取图像,因为当我第一次单击时,这不起作用onload=“UploadImage_Complete()”,但JSON发送结果并显示为什么我需要事件来获取信息,当JSON被信任时,或者更改结果来执行此方法UploadImage_Complete();
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";
}
}
}
@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>
[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"))
}
};
}