About JS calling the external deployment Web API

Source: Internet
Author: User

I didn't think I'd have to continue writing these things for years. Waterfall Sweat ~

Recently had to start research with Web API

MVC project, in the JS file, implementation click on a button to call the external published API, and then from the API to the value back to the JS page, jump.

The following two methods have been tested to obtain the API return value effectively.

function TestInfo (ID) {$.ajax ({URL:"Http://158.14.51.103/api/Verify/Get", type:'GET', DataType:'JSON', data: {id:id, Key:"123456", FURL:"./salessetting"}, Success:function (data) {alert ("123"+data.            FURL); Console.log (data.        FURL);    }    }); $.Get('Http://158.14.51.103/api/Verify/Get', {id:id,key:"123456", FURL:"./salessetting"}, function (Result) {Console.log (result).    Key); });}

However, the first time you call the API, you keep reporting the following error.

XMLHttpRequest cannot load http://158.14.51.103/api/Verify/Get? Id=30054&key=123456&token=&furl=.%2fsalessetting. No ' Access-control-allow-origin ' header is present on the requested resource. Origin 'http://localhost:5955' is therefore not allowed access. The response had HTTP status code 404.

Check the online a lot of people say is Chome browser problem, but in the browser properties Riga Access-control-allow-origin string is not easy to use, and later found the following this URL, some people in foreign countries are asking this question.

http://stackoverflow.com/questions/27504256/mvc-web-api-no-access-control-allow-origin-header-is-present-on-the-requested

You need the Enable CORS in your Web Api. The easier and preferred to enable CORS globally are to add the following into Web. config

<system.webServer>  

Please note that the Methods is all individually specified, instead of using * . This is because there are a bug occurring when using * .

You can also enable CORS by code.

Add the above code to the config to solve the ~ touched!

The following page also mentions this issue

http://blog.csdn.net/starfd/article/details/45307659

2. config method to achieve cors

The benefit of adding configuration under the System.webserver configuration section of the Web. config is simple, as long as this configuration is added here, all APIs can support cross-domain requests by the same rule

 
    <Httpprotocol>        <customheaders>          <Addname= "Access-control-allow-origin"value="*" />          <Addname= "Access-control-allow-headers"value="*" />          <Addname= "Access-control-allow-methods"value= "GET, POST, PUT, DELETE" />        </customheaders>      </Httpprotocol> 

If it is HTTP Basic Access authentication, it also seems to require a <add name= "Access-control-allow-credentials" value= "true"/> Not yet verified here

Finally add a cors related note: http://www.cnblogs.com/artech/p/cors-4-asp-net-web-api-02.html

and official Links: Http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

In addition, about the deployment of Web API on IIS, I just want to shout--IIS if it is post-loaded remember to register remember to register remember to register !!!

Well, I hope I can under the persecution of the Web API, the smooth live to next week!!!

About JS calling the external deployment Web API

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.