简体   繁体   中英

AngularJs $http deforms request url

I'm pretty new to AngularJS, but for some reason i have to use it and create small web-page, to be able to sent some requests to my Web Api 2 part. I`m stuck with one strange error. My Controller is in ApplicationFrontEnd/MainView folder, file is called "main.js". Problem part of code :

    var request = {
        method: 'POST',
        url: 'api/login',
        headers: {
            'Authorization': 'Basic ' + encoded
        }
    }
    $http(request)
       .success(function (data, status, headers, config) {
          // I`m supposed to have no problem
       })
       .error(function (data, status, headers, config) {
           //but for some reason i do have.
       });

So, at the point of sending post request is sent not to 'api/login' , but to 'ApplicationFrontEnd/api/login' , and do not have any Authorization header. On Web Api part of project i have CORS turned on, etc., but while i have wrong url in request, i receive 404 error (pretty logical). Can someone please explain to me, what i'm doing wrong and how to fix it?

Make the url absolute by adding a leading /

url: '/api/login'

Will make calls to http://yourDomain.com/api/login .

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