Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Node API server with CORS disabled, AngularJS controllers with proxy and no proxy, and the ionic.project settings to allow proxy
var express = require('express')
var app = express()
app.get('/api/feed', function(req, res) {
res.json({name: 'feed', items: ['first', 'second']})
})
var server = app.listen(3000, function () {
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
})
//Say we ran `ionic serve`
//This will access proxy, and pass along feed correctly
angular.module('starter.controllers', [])
.controller('DashCtrl', function($scope, $http) {
$http.get('/api/feed').then(function(data) {
console.log('data ' , data)
})
})
//This will not access proxy, instead sending request straight across origin
//causing a failure to happen.
// XMLHttpRequest cannot load http://0.0.0.0:3000/api/feed.
//No 'Access-Control-Allow-Origin' header is present on the requested resource.
//Origin 'http://localhost:8100' is therefore not allowed access.
.controller('FeedCtrl', function($scope, $http) {
$http.get('http://0.0.0.0:3000/api/feed').then(function(data) {
console.log('data ' , data)
})
})
{
"name": "1test",
"app_id": "",
"proxies": [
{
"path": "/api",
"options": "http://0.0.0.0:3000/api"
}
]
}
@droiddavid

This comment has been minimized.

Copy link

@droiddavid droiddavid commented Jun 12, 2016

Hello, I wonder if you can help me. I am desperate at this point. I have not been able to develop for more than two and a half weeks now. The only thing I have changed is that I am using ionic. I am having the CORS issue and tried everything under the sun on both the server side and front end. I am using XAMPP 2.4.

My directory structure is as follows...
c:\xampp\htdocs\neighborfood
|-www
||-database
|||-getUser.php
||-js
|||-app.js
||--index.html
||--ionic.project

ionic.project entire listing...
{
"name": "neighborfood",
"app_id": "1.0.0.0",
"email": "myEmailAddress@gmail.com",
"proxies": [
{
"path": "/#/database", (I've also tried /database)
"proxyUrl": "http://localhost:8080/database"
}
]
}

app.js -- code from within my controller. It's just the login function...
$scope.login = function (user) {
$http.get(
'/database/getUser.php',
{'email': 'flash@gordon.com','password': 'locutus',
'contentType': 'application/json; charset=utf-8',
'dataType': 'json'
}).success(function (response) {console.log(response);});
}; //$scope.login

When I perform ionic serve, the site starts without issue. However, when I click the 'LOGIN' button, the response returned is the contents of getUser.php (the actual php code). The php code is not being parsed. Instead it is being displayed as if it were text. This proxy coding is my last attempt. If this doesn't work, then I am scrapping ionic altogether as two plus weeks of no coding is killing my project. I would be grateful for any help you can provide. Thank you so very, very much!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment