Using Javascriptresult in ASP. NET MVC

Source: Internet
Author: User
Tags getscript

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.