jQuery Ajax authorization request header - javascript

I have a problem with AJAX request to my external API.
I tried to develop login from jQuery to API like that:
$.ajax({
type: "GET",
dataType: "json",
crossDomain: true,
cache: false,
url: __Configuration.apiURL+"/login",
xhrFields: {
withCredentials: true
},
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", Login.MakeAuth(email, password));
},
success: function(data){
}
});
Everything looks ok when login and username are correct,
but when i tried to put wrong login and password then Ajax returns authorization window (prompt) from api.. so my problem is: how to prevent from that? I don't want to see this prompt...

Related

Ajax request Gets redirected (302 response) (due to passport i guess)

so I'm trying to send a formula with Ajax, before using passport for the Signup it was working, after using passport, the POST request to "/ajout-membre" gets redirected to "/" with a GET, explain more my code.
var url = "http://localhost:8080/ajout-membre";
$.ajax({
url: url,
headers: {"X-CSRF-Token": $("#csrf1").val()},
data: JSON.stringify(sentinfo),
xhrFields: {withCredentials: true},
crossDomain: true,
type: 'POST',
contentType: 'application/json',
success: function(data){
//here something to do;
if(data=="membre_existant")
alert("membre existant");
else{
alert("Une erreur inconnue s'est produite");
}
}
});
The "/ajout-membre" just for the test:
router.post('/ajout-membre',function(req,res){
res.send(JSON.stringify("Done"));
});
how you can help me to fixe this, i just want him to got to that root that's all

Jquery: Redirect Page

$.ajax({
type: "POST",
url: "http://localhost:8080/ChecktToken",
data: {"token": ($.cookie("uid")) },
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
success: function() {
},
error: function(data) {
location.href =('login.html')
},
});
The above Ajax call is used for redirecting the user to the login page if he is trying to access any other pages before actually logging into an application.
This works completely OK, but it takes about 2 seconds to redirect to the login page.
So for example if I try to access http://localhost:8080/index.html prior to logging in I will be able to see that page for 2 seconds before actually being redirected.
Is location.href =('login.html') the best method for redirection or is there something better I could do?
Looking for advices.
2 second delay in redirecting page is due to Ajax call waiting for response from server. These 2 seconds may increase if network of server slows down. To prevent page load you have hide page till you get the response or error. You can do that using beforeSend call back in ajax.
$.ajax({
type: "POST",
url: "http://localhost:8080/ChecktToken",
data: {"token": ($.cookie("uid")) },
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
beforeSend: function() {
//Hide page till you get the response. You can use element Id which holds entire body.
$(body).hide();
},
success: function() {
$(body).show();
},
error: function(data) {
location.href =('login.html')
},
});

Ajax don't return response

Hello i trying to to save response from htmlpage and save the content of this page, but i always getting an error.What i am doing wrong?
<script type="text/jscript">
var page = "";
$.ajax({
url: "https://www.bbvanetcash.com/local_kyop/KYOPSolicitarCredenciales.html",
type: "GET",
cache: false,
crossDomain: true,
cache: false,
contentType: "application/json; charset=utf-8",
data: {},
jsonp: 'jsonCallback',
dataType: "jsonp",
success: function (data) {
page = data;
console.log("Good");
},
error: function (e) {
console.log("Error");//here where i am stuck
}
});
</script>
"https://www.bbvanetcash.com/local_kyop/KYOPSolicitarCredenciales.html" - i think its URL belong from other domain not from your home/base domain url. that's why you getting error.
You can not make a AJAX http request for NON HOME url.
NON HOME: Its means you can only access your Base domain content though ajax.

Ajax appending nonce to js files

I'm getting a unique nonce (i think a timestamp) appended to all and any of my js files every time I send a request to my rails web service:
I'm fairly sure the error has to do with the ajax call, because of jquery.ajax documentation which states cache:true in your ajax request will stop ajax from appending "_={timestamp}" to the get parameters.
I have set cache: true with no luck. How else can I stop these nonces from being applied?
ajax:
$.ajax({
beforeSend: function (xhr) {
xhr.setRequestHeader ('Authorization', api_key);
},
cache: true,
dataType: 'html',
type: 'GET',
url: url+'/gwsearch/ajax_search?d1='+d1_val+'&d2='+d2_val,
crossDomain: true,
success:function(result) {
$("#display").html(result);
},
error: function(result) {
$('#display').html('Unauthorized client.');
}
});
I solved this using an ajax prefilter:
$.ajaxPrefilter('script', function(options) {
options.cache = true;
});

How to set cookie value with AJAX request?

I want to set a cookie value on an AJAX request but the code below doesn't work.
$.ajax({
type: "GET",
url: "http://example.com",
cache: false,
setCookies: "lkfh89asdhjahska7al446dfg5kgfbfgdhfdbfgcvbcbc dfskljvdfhpl",
crossDomain: true,
dataType: 'json',
success: function (data) {
alert(data);
});
How can I set cookies in the header?
Basically, ajax request as well as synchronous request sends your document cookies automatically. So, you need to set your cookie to document, not to request. However, your request is cross-domain, and things became more complicated. Basing on this answer, additionally to set document cookie, you should allow its sending to cross-domain environment:
type: "GET",
url: "http://example.com",
cache: false,
// NO setCookies option available, set cookie to document
//setCookies: "lkfh89asdhjahska7al446dfg5kgfbfgdhfdbfgcvbcbc dfskljvdfhpl",
crossDomain: true,
dataType: 'json',
xhrFields: {
withCredentials: true
},
success: function (data) {
alert(data);
});

Categories