Enter the address in the browser address bar, on the page want to pop a box through the script, see the Controller under a JavaScript method, the type returned is Javascriptresult, so you want to use this method popup box,
Public actionresult Index () { return this. JavaScript ("<script>alert" ("Successful Operation") </script>"); }
When you visit a page, the JS text is displayed on Firefox.
On IE is the download file
To view the response header information, the Content Type isapplication/x-javascript,返回的是js代码。
Therefore, this is used when the page cannot be accessed. JavaScript output script, popup prompt box.
You can use this. Content output script, pop-up prompt box, like this
Public actionresult Index () { return this. Content ("<script>alert (' Operation Success ') </script>"); }
Of course, you can also write JS on the view page, the same can achieve the same effect.
So, how to use Javascriptresult, you can use the JQuery method on the page getScript, get the JS code to the server, and then execute the JS code, so the service-side code is changed to this
Public actionresult Index () { returnthis. JavaScript ("alert (' Operation succeeded '); " ); }
Note that you do not need to add <script> tags, so do not write this. JavaScript ("<script>alert (' Operation succeeded ');</script>").
So write on the Index2 page, request/home/index
<script> $.getscript ("/home/index"); </script>
The last visit to the/home/index2 page will pop up a box
So this. What's the difference between content and This.javascript?
This. The return type of content is contentresult,this. The return type of JavaScript is Javascriptresult,
Decompile Contentresult and Javascriptresult, you can see the difference
Javascriptresult set Contettypew to Application/x-javascript, and Contentresult can customize ContentType,
In this case, you can use this. The Content ("alert (' Operation succeeded ')", "Application/x-javascript,") replaces this. JavaScript ("Alert (' Operation succeeded ')")
public ActionResult Index () {
return this . JavaScript ( alert (' Operation succeeded '); " ); public ActionResult Replacecontentmethod () { return this . Content ( alert (' Operation succeeded '); , " application /x-javascript ); }
Then on the INDEX2 page use getscript respectively request/home/index,/home/replacecontentmethod, see the effect
The response headers for/home/index are as follows
The response headers for/home/replacecontentmethod are as follows
As you can see, the two are the same.
Using Javascriptresult in ASP. NET MVC