how can I authenticate to a rest web service by ajax - javascript

I want to send username and password to a Rest web service to recover the Token. I tested this code but always sends me to error function
$.ajax({
type: "POST",
url: "http://192.168.1.89:1111/connect/token",
dataType: 'json',
async: false,
data: '{}',
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization', btoa(username, password));
},
success: function() {
alert('Thanks for your comment!');
},
error: function(xhr, status, error) {
alert(error);
}
});

Related

error 500 in ajax call when call to the server, but in localhost works

I have a js when i do a ajax call to one server. When i call to this service using localhost, works. But when i call to this service using the server where i upload it, its ERROR 500. The call return a json.
$.ajax({
url: "https://www.example.com/example",
dataType: "json",
data: {
'data': xml,
'message': message,
'customer_id': customer_id,
'subscr_id': subscr_id
},
type: 'POST',
success: function (devol) {
},
error: function (xhr, ajaxOptions, thrownError) {
alert("no ha entrado");
}
});
Try to update your code below:
var model = {
'data': xml,
'message': message,
'customer_id': customer_id,
'subscr_id': subscr_id
};
$.ajax({
url: "https://www.example.com/example",
type: 'POST',
dataType: 'json',
data: JSON.stringify(model),
//headers: { 'authorization': `Bearer ${token}` },
async: false,
processData: false,
contentType: "application/json",
error: function (err) {
},
success: function (data) {
}
});
It might be because of CORS (Cross-Origin Resource Sharing)... Usually, you cannot make calls to other domains from the browser unless the other domain you are making calls allows CORS to all sites or specific sites.
enter link description here

Why Unable to get success callback in Jquery/ajax call to RESTful services?

function GET() {
jQuery.support.cors = true;
$.ajax({
url: 'http://localhost:32253/api/UserDetail/GetRoleDetails',
type: 'GET',
async:true,
contentType: "application/json",
dataType: 'json',
xhrFields: {
withCredentials: true
},
error: function (xhr, status)
{
alert(status);
},
success: function (data) {
alert("Success!");
}
});
return false;
}
I am very much new to ajax,I tried a ajax call to my services method which returns value.But the success callback function is never fired .I only get error.What might be the reason?
I tried using dataType to jsonp and other similar solutions which had been found in Stackoverflow regarding this issue.Nothing worked out.I am getting server response as 200 oK.
try this code
$.ajax({
type: "POST",
url: URL,
async: true,
contentType: "application/json; charset=utf-8",
dataType: "json",
jsonp: "onJSONPLoad",
jsonpCallback: "newarticlescallback",
crossDomain: "false",
beforeSend: function (xhr) {
},
error: function (request, status, error) {
console.log("Error In Web Service...." + request.responseText);
return false;
},
success: ajax_success,
complete: function (xhr, status) {
}
});
create function "ajax_success" in ur page
like below
function ajax_success(parsedJSON) { //you code here
}
this may be help you
try this code
$.ajax({
type: "POST",
url: URL,
async: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
jsonp: "onJSONPLoad",
jsonpCallback: "newarticlescallback",
crossDomain: "false",
beforeSend: function (xhr) {
},
error: function (request, status, error) {
console.log("Error In Web Service...." + request.responseText);
return false;
},
success: function (data) {
Result = data;
},
complete: function (xhr, status) {
}
});

Send headers in JQuery Ajax Get Request

Here is my code.
$.ajax(this.url, {
type: "GET",
//dataType: "json",
beforeSend: function (xhr) {
xhr.setRequestHeader("x-token", token)
},
success: function (data) {
console.log(data);
},
error: function(){
console.log('error');
}
});
I am not able to send ajax request by that code. I also try hearder: {"x-token": token}, In place of beforeSend: but its also not working for me.

Reliably complete Ajax request on page unload

I have a html/javascript application. When user refreshes the page or close the page i need to make a ajax request and then close the application.
I am using page unload event for this purpose.
I am calling
window.onbeforeunload=beforeFunction;
beforeFunction will make the ajax request. But when i check in fiddler i dont see the ajax request. However if i debug the application and execute each line with f10 then i see the ajax request in fiddler.
thats how my ajax request is formed:
$.ajax({
url: url,
type: "GET",
contentType: "application/json",
async: false,
crossDomain: true,
dataType: 'jsonp',
success: function(json){
alert("success: " + json);
},
error: function(xhr, statusText, err) {
alert("Error:" + xhr.status);
}
});
$(window).on('beforeunload' function() {
$.ajax({
url: url,
type: "GET",
contentType: "application/json",
async: false,
crossDomain: true,
dataType: 'jsonp',
success: function(json){
alert("success: " + json);
},
error: function(xhr, statusText, err) {
alert("Error:" + xhr.status);
}
});
});
Try this....

Ajax setRequestHeader is not working

I am trying to add header to ajax request.
I use setRequestHeader but it do nothing. I can look in firefox that request headers doesnot contains Authentication! What i am doing wrong?
And how to add header?
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://test/test.svc/news",
dataType: "json",
beforeSend : function(xhr) {
xhr.setRequestHeader("Authentication", "Basic " + Base64.encode(username + ":" + password))
},
success: function(data) {
alert('Success');
},
error: function() {
alert("Loading data error...");
}
});
}
Try username and password instead
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://test/test.svc/news",
dataType: "json",
username: 'username', /* not user */
password: 'pass',
success: function(data) {
alert('Success');
},
error: function() {
alert("Loading data error...");
}
});
}
username -
A username to be used in response to an HTTP access authentication request

Categories

Resources