Created
December 8, 2014 19:29
-
-
Save jbavari/d9c1c94058c4fdd4e935 to your computer and use it in GitHub Desktop.
Node API server with CORS disabled, AngularJS controllers with proxy and no proxy, and the ionic.project settings to allow proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "1test", | |
"app_id": "", | |
"proxies": [ | |
{ | |
"path": "/api", | |
"options": "http://0.0.0.0:3000/api" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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": "[email protected]",
"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': '[email protected]','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!!!!