AJAX REST API request - javascript

My problem is that I can not send the correct request to the server to get an answer from him in the form of a json line. Here is my code:
$.ajax({
type: "GET",
url: url,
dataType: 'json',
crossDomain: true,
username: 'username',
password: 'password',
headers: {
'Access-Control-Allow-Origin': '*'
},
success: function(response) {
console.log(response);
}
})
When I try to send a request, I get an error:
XMLHttpRequest cannot load URL. Response to preflight request doesn't
pass access control check: No 'Access-Control-Allow-Origin' header is
present on the requested resource. Origin 'null' is therefore not
allowed access.

var dataSet = {
username: 'username',
password: 'password',
}
$.ajax({
type: "GET",
url: url,
dataType: 'json',
crossDomain: true,
data: dataSet,
// the origin header has to be set server side
/*
headers: {
'Access-Control-Allow-Origin': '*'
},
*/
success: function(response) {
console.log(response);
}
});
parameters have assigned by a dataSet and the option data.
the allow origin header has to be set server side, not in the client

Related

Looking for help about preflight request. Getting error on ajax request. Below is my request code and error

Access to XMLHttpRequest at 'https://www.site_a' from origin 'https://fb66da46a53d.ngrok.io' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
$.ajax({
url: 'https://www.site_a',
type: 'POST',
headers: {
'Access-Control-Allow-Origin': 'https://fb66da46a53d.ngrok.io',
},
data: {
produit: 'vente',
geo_objets_ids: '8979',
typesbien: 'appartement',
typesbien: 'terrain',
nb_pieces: 1,
nb_pieces: 2,
nb_pieces: 3,
nb_chambres: '',
surface: '',
prix: '',
action: 'submit'
},
success: function(response) {
// window.location.href = response;
console.log(response);
}
});

The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:8080, http://localhost:8080, http://localhost:8080'

The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:8080, http://localhost:8080, http://localhost:8080', but only one is allowed. Origin 'http://localhost:8080' is therefore not allowed access.
My code is:
$.ajax({
url: "http://tilesdev1.intra.schneider.com/api/fa-snihelp-service/entserv",
type: "GET",
dataType: 'json',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
},
xhrFields: {
withCredentials: true
},
crossDomain: true,
success: function (result) {
debugger;
},
error: function (xhr, textStatus, error) {
console.error(error);
},
});

CORS: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin'

I'm trying to login to moodle from an external webpage using a post form to moodle, I used the next ajax to send the inputs:
var frm = $('#loginForm');
frm.submit(function(e) {
e.preventDefault();
$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
xhrFields:{
withCredentials:true
},
async:true,
beforeSend: function (xhr){
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
},
success: function (data) {
console.log("Logged");
},
error: function (data) {
console.log("NOT Logged");
},
});
});
Now into the moodle's login/index.php I insert the headers to make possible the CORS connection:
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: http://fabianmurillo.000webhostapp.com");
header("Origin" : "http://fabianmurillo.000webhostapp.com");
When I run the code, the browser returns an error:
..preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin'..
enter image description here
Dunno why browser is blocking the connection for login to moodle.
Thanks for your help.

Jquery authentication error

Consider the following code:
var jsonData1 = $.ajax({
url: 'http://'+domainName+':9898/jolokia/read/*',
dataType:"json",
crossDomain: true,
beforeSend: function(xhr){
var username= '*';
var password= '*';
xhr.setRequestHeader("Authorization",
"Basic " + btoa(username + ":" + password));
},
async: false
}).responseText;
var parsed1 = JSON.parse(jsonData1);
Here when I directly access the url, it asks for a username and password, which when given shows the value.
But when I do it through an ajax call, it throws this error:
Failed to load resource: the server responded with a status of 401 (Unauthorized)
jquery.min.js:5 XMLHttpRequest cannot load http://10.91.240.21:9898/jolokia/read/* No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 401.
(index):1 Uncaught SyntaxError: Unexpected token u
TRY 2:
var jsonData2 = $.ajax({
type:'GET',
url: 'http://'+domainName+':9898/jolokia/read/*',
dataType:"jsonp",
crossDomain: true,
data: {username: '*',password: '*'},
async: false
}).responseText;
var parsed2 = JSON.parse(jsonData2);
I tried using json p.
The unauthorized code got resolved. It is showing status OK. But now this error comes
Uncaught SyntaxError: Unexpected token u
I think I got the answer. The cross domain issue is resolved.
$(document).ready(function() {
var surl = "http://www.anotherdomain.com/webservice.asmx";
$.ajax({
type: 'GET',
url: surl,
crossDomain: true,
contentType: "application/json; charset=utf-8",
data: { UserID: 1234 },
dataType: "jsonp",
async: false,
cache: false
});
});

Issue sending AJAX request to another domain

I am trying to use this API: https://zipcodedistanceapi.redline13.com/API
But when I make the AJAX call, I'm getting this error:
XMLHttpRequest cannot load
https://zipcodedistanceapi.redline13.com/rest//radius.json/30341/10/mile.
No 'Access-Control-Allow-Origin' header is present on the requested
resource.
Here is the request:
$.ajax({
type: "GET",
url: 'https://zipcodedistanceapi.redline13.com/rest/<api key goes here...>/radius.json/30341/10/mile',
dataType: "json",
success: function(zipback) {
}
});
I know it has something to do with making the request from a different domain, but I don't not how to resolve the issue.
$.ajax({
type: "GET",
url: 'https://zipcodedistanceapi.redline13.com/rest/<api key goes here...>/radius.json/30341/10/mile',
dataType: "json",
jsonpCallback:'somename',
success: function(zipback) {
}
});

Categories

Resources