WEBAPI Cross-domain requests

Source: Internet
Author: User

In the actual development will provide WEBAPI to the front-end JS Direct call situation, there will be a cross-domain situation,

Solution:

Add code to Global

   protected voidApplication_BeginRequest (Objectsender, EventArgs e) {HttpContext.Current.Response.AddHeader ("Access-control-allow-origin","*"); if(HttpContext.Current.Request.HttpMethod = ="OPTIONS") {HttpContext.Current.Response.AddHeader ("Access-control-allow-methods","*"); HttpContext.Current.Response.AddHeader ("access-control-allow-headers","*");            HttpContext.Current.Response.End (); }        }

In the configuration file, add

      <remove name= "Optionsverbhandler"/>      <remove name= "Traceverbhandler"/>
   <handlers>      <Removename= "extensionlessurlhandler-integrated-4.0" />      <Removename= "WebDav" />       <Removename= "Optionsverbhandler" />      <Removename= "Traceverbhandler" />      <Addname= "extensionlessurlhandler-integrated-4.0"Path="*."verb="*"type= "System.Web.Handlers.TransferRequestHandler"Precondition= "integratedmode,runtimeversionv4.0" />    </handlers>

You can do it.

  $.ajax ({                ' Http://api.zxsj.com/api/Job?strName=liuyl ',                "Get",                 "json ",                data: {                    //                },                true,                 function (res) {                    $ (". Code"). Text (res. Code);                    $ (". Mess"). Text (res. Message);                }            );

WEBAPI Cross-domain requests

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.