Error making http ionic request

1

I am doing an ionic test in which I make an http request to a service.

This service does not have CORS implemented, so I have to create a proxy to avoid this (this is what I have been told, I think that is the case).

My app is very simple, I just want to make the http request I said earlier.

To make my app I have done what is indicated on this website: link

In this example a request is made to a website that has CORS implemented, so the request does not give problems but when doing it on my server it gives the following error:

  

"Source link not found in the header   Access-Control-Allow-Origin "

To solve this, I tried what it says in the following link: link

According to this summary, I have left my file packaje.json in the following way:

{
  "name": "pruebahttp1",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "http://ionicframework.com/",
  "private": true,
  "scripts": {
    "clean": "ionic-app-scripts clean",
    "build": "ionic-app-scripts build",
    "lint": "ionic-app-scripts lint",
    "ionic:build": "ionic-app-scripts build",
    "ionic:serve": "ionic-app-scripts serve",
    "start": "ng serve --proxy-config proxy.conf.json" <--Esto es lo que he añadido
  },
  "dependencies": {
    "@angular/common": "5.0.3",
    "@angular/compiler": "5.0.3",
    "@angular/compiler-cli": "5.0.3",
    "@angular/core": "5.0.3",
    "@angular/forms": "5.0.3",
    "@angular/http": "5.0.3",
    "@angular/platform-browser": "5.0.3",
    "@angular/platform-browser-dynamic": "5.0.3",
    "@ionic-native/core": "4.4.0",
    "@ionic-native/splash-screen": "4.4.0",
    "@ionic-native/status-bar": "4.4.0",
    "@ionic/pro": "1.0.20",
    "@ionic/storage": "2.1.3",
    "ionic-angular": "3.9.2",
    "ionicons": "3.0.0",
   "rxjs": "5.5.2",
    "sw-toolbox": "3.6.0",
    "zone.js": "0.8.18"
  },
  "devDependencies": {
    "@ionic/app-scripts": "3.1.8",
    "typescript": "2.4.2"
  },
  "description": "An Ionic project"
}

And I created the proxy.config.json file like this:

{
"/api": {
    "target": "http://195.235.55.21:50072",
    "secure": false
}
}

{
"/angular": {
 "target":  {
   "host": "195.235.55.21",
   "protocol": "http:",
   "port": 50072
 },
 "secure": false,
 "changeOrigin": true,
 "logLevel": "info"
 }
}

To run the app so far I used ionic serve -l but now it tells me that I have to use the npm start command and when I use it, it does not run the app and it tells me the following:

  
    

ng serve --proxy-config proxy.conf.json

  
     

As a forewarning, we are moving the CLI npm package to "@ angular / cli"   with the next release, which will only support Node 6.9 and greater.   This package will be officially deprecated shortly after.

     

To disable this warning use "ng set --global   warnings.packageDeprecation = false. "You have to be inside an   angular-cli project in order to use the serve command. npm ERR! code   ELIFECYCLE npm ERR! errno 1 npm ERR! [email protected] start: ng serve --proxy-config proxy.conf.json npm ERR! Exit status 1 npm ERR!   npm ERR! Failed at the [email protected] start script. npm ERR! Este   is probably not a problem with npm. There is likely additional logging   output above.

     

npm ERR! A complete log of this run can be found in: npm ERR!
  C: \ Users \ User \ AppData \ Roaming \ npm-cache_logs \ 2018-03-12T11_26_14_674Z-debug.log

Any ideas to fix it? Thanks

    
asked by Pablo Simon DiEstefano 12.03.2018 в 12:28
source

1 answer

1

For what I read in the official Ionic documentation , instead of using the CLI of angle is used one of ionic (instead of ng serve is used ionic serve ), which makes the configuration is slightly different:

Example:

  • You have your application with Angular that is loaded from http://localhost:4200/ .
  • This application requires AJAX calls to http://aqui.com/api/<nombre_recurso> .

Therefore, in the file ionic.config.json add an entry like the following:

"proxies": [
  {
    "path": "/api",
    "proxyUrl": "http://aqui.com/api"
  }
]

And, in your code, when you use http.get('/api/v1/usuarios') , your local server will receive that request and redirect it to http://aqui.com/api/v1/usuarios . That way, for the browser everything comes from the same site and you do not have to worry about CORS.

To launch the server, you still use ionic serve

    
answered by 12.03.2018 / 12:45
source