I'm trying to make an Ajax call from my client like that:
$.ajax({
url: '/item/' + id + '/action?parameter=' + parameter,
method: 'GET',
success: function(data) {
.....
}
});
and the error I'm getting is:
500 (Internal Server Error) (from the Azure server
(c["X-Requested-With"]="XMLHttpRequest");for(e in c)void 0!==c[e]&&f.setRequestHeader(e,c[e]+""); (highlight)
The rest of the site is working well, only the ajax call returs the error (tried also with POST)
In my server, I've done few attempts:
1)
cors = require('cors'),
path = require('path');
var app = express();
app.use(cors());
2)
exports.action= function(req, res) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
But still getting the error
Related
I am trying to get some data using ajax in laravel project.
i have https domain but when the ajax request is made it is made over http.
So the ajax request gives a mixed content error and also a https request for same is showed cancelled.
i have already added to app service provider
if(config('app.env') === 'production') {
\URL::forceScheme('https');
}
main ajax setup with url formation
let url = '/opportunities/?' + $('#opportunity_search_form').serialize();
if(window.location.search && !this.initialLoaded){
url = url + '&' +window.location.search.substring(1);
}
url = url + '&page=' + self.pageCtr
$.ajax({
url: url,
type: "get",
beforeSend: function(xhr, type) {
if (!type.crossDomain) {
xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
}
}
})
in the above image first request that shows cancelled goes over https ... and the other goes over http with mixed content error..i don't know how it is being sent .
Also i tried creating url using
let url = window.location.protocol + '//' + window.location.hostname + '/opportunties/' + other parameters
Thankyou.
I'm using Node JS as a proxy server. In JavaScript, after hitting a button, I'm sending an ajax post to one of my routes. Here's the ajax post:
var requestData = {myData: blablaa,....};
$.ajax({
type: 'POST',
data: JSON.stringify(requestData),
url: '/my-route'
}).done(function(data) {
console.log('returned data:', data);
});
And then my route looks like this:
var express = require('express');
var router = express.Router();
var request = require('request');
router.post('/refund-request', checkSession, function (req, res) {
var urlEndpoint = "some.url.endpoint";
request(urlEndpoint).pipe(res);
});
But I'm getting a 505 error on that.
In my route, how do I properly send back a response (like I would get if posting to an actual url)?
My ajax call is as follows
var data = {};
data.name = name;
data.phone = phone;
data.address=address;
$.ajax({
type: 'POST',
data: JSON.stringify(data),
contentType: 'application/json',
url: 'http://localhost:5000/endpoint',
success: function(data) {
console.log('success');
console.log(JSON.stringify(data));
}
});
and I am using node-js express server
and my server code is
app.post('/endpoint', function(req, res){
var obj = {};
console.log('body: ' + JSON.stringify(req.body));
res.send(req.body);
});
app.listen(5000);
I have tried the other solutions posted but I still keep getting this error
Response to preflight request doesn't pass access control check: No Access-Control-Allow-Origin header is present on the requested resource. Origin http://localhost:3000 is therefore not allowed access.
What do I need to do ?
Before your routing on your server, add :
app.all('/*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
});
You can replace the * with the URL of authorized domains.
More informations : https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
I wrote very simple server :
/* Creating server */
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
/*Start listening*/
server.listen(8000);
I run it using nodejs.
Now i want to write simple client that use ajax call to send request to server and print response (Hello World)
Here javascript of clinet:
$.ajax({
type: "GET",
url: "http://127.0.0.1:8000/" ,
success: function (data) {
console.log(data.toString);
}
});
When I open client html file i get following error in console:
XMLHttpRequest cannot load http://127.0.0.1:8000/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
I tried adding to ajax call following:
$.ajax({
type: "GET",
url: "http://127.0.0.1:8000/" ,
dataType: 'jsonp',
crossDomain: true,
success: function (data) {
console.log(data.toString);
}
});
But then i get
Resource interpreted as Script but transferred with MIME type text/plain: "http://127.0.0.1:8000/?callback=jQuery211046317202714271843_1410340033163&_=1410340033164".
Anyone can explain what i did wrong and perhaps how to fix it?
Many thanks!
To overcome the CORS, in your node.js file write the below, based on what you need:
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
The first error is caused by CORS (Cross Origin Resource Sharing) policy. It's rule by all browsers that you cannot make a request to a remote server in AJAX other than to the current server the script/page was loaded from unless that remote server allows it via Access-Control-Allow-Origin header.
I suggest serving the page from the same Node.js server. Then it will work. Example, when the request comes to root / page, then serve the index.html file, otherwise, server whatever other content you want.
var http = require('http'),
fs = require('fs');
/* Creating server */
var server = http.createServer(function (request, response) {
if (request.url == '/' || request.url == '/index.html') {
var fileStream = fs.createReadStream('./index.html');
fileStream.pipe(response);
} else {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
}
});
/*Start listening*/
server.listen(8000);
I am trying to invoke linked rest api from my angularjs web app I am using $http for this below is the code:
url = "https://api.linkedin.com/v1/people/~?oauth2_access_token=" + $rootScope.accessToken;
$http({ method: 'GET', url: url, data: {} }).success(function (data, status) {
}).error(function (data, status) {
//the code always comes here with data ,status being null
});
but if i paste the url along with my accessToken in the browser than i get the expected json response. so please tell me what am I missing in this request. thanks!