简体   繁体   中英

Trying to send a HTML document from ASP.NET MVC to JavaScript and show with jQuery

I am trying to send a html document from ASP.NET MVC to JavaScript, and then populate a Kendo windo in an iFrame with the new data.

Here's what works. I can make the pop up window's content complete replaced with a simple hardcoded html page after a response comes back from the controller:

  function formSubmit() {
      $.post($('form').attr("action"), $("form").serialize(), function(data){
                    document.write("<html><head></head><body>this is a test</body></html>");
                }
            }
        })
  }

If I return some simple HTML from the controller, I can see that html in an alert box. However, it doesn't work if I put it in the document.write call. For example, here's the JavaScript:

  function formSubmit() {
      $.post($('form').attr("action"), $("form").serialize(), function(data){
                    alert(data); // I see exactly what the controller passed back
                    document.write(data); // The pop-up page becomes blank, as if the data passed to this function is invalid.
                }
            }
        })
  }

In my controller, I have

    public ActionResult Create(VMReportObservation viewModel)
    {
        return Json("<html><head></head><body>hello</body></html>");
    }

On the JavaScript side, is there some sort of conversion to a string that I need to do to make the document.write() call work? Or is there something else I'm missing?

You are returning JSON. Instead,

return Content("<html>...</html>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM