Whenever I encounter a CORS issue while developing a webapp, my go-to solution is to brew some coffee. However, after struggling with it for some time, I am unable to resolve the problem this time and need assistance.
Below is the client-side code snippet:
$http({method: 'GET', url: 'http://localhost:3000/api/symbol/junk',
headers:{
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With',
'X-Random-Shit':'123123123'
}})
.success(function(d){ console.log( "yay" ); })
.error(function(d){ console.log( "nope" ); });
The server-side is a typical node.js application with an express framework. I have incorporated the cors extension into express in the following manner:
var app = express();
app.configure(function(){
app.use(express.bodyParser());
app.use(app.router);
app.use(cors({origin:"*"}));
});
app.listen(3000);
app.get('/', function(req, res){
res.end("ok");
});
When I run the command
curl -v -H "Origin: https://github.com" http://localhost:3000/
The response is as follows:
[Output from curl command]
Executing the client-side code results in the following error message:
Error Message
Upon inspecting Chrome's headers, I noticed that..
[Chrome Headers Analysis]
Updates:
I made adjustments by switching to jQuery on the frontend and modifying the backend code as shown below:
[Updated Backend Code]
Although GET requests are now functioning properly, other methods like PUT and POST are still causing issues. I am open to suggestions and solutions before resorting to solely using GET requests for all interactions.