I have a code which tries to post to a api. But I keep getting 401 in firefox and 400 in safari
$.ajax({
type: "POST",
url: "http://url.com",
dataType: "json",
async: "false",
data: form_data,
beforeSend: function(xhr1) {
xhr1.setRequestHeader("Authorization", "Basic " + encodeBase64(username + ":" + password))
},
success: function (data,status,xhr){
//do something
}
});
I followed instructions from here
http://dothow.blogspot.com/2009/05/http-basic-authentication-with-jquery.html?showComment=1270999665472#c3021624182011440325
*encodeBase64 is part of a library function
Related
I have used ajax calling to get details from database this ajax calling works fine in local but after uploading on server I always get error Resource Cannot be found I have tried many things depend on the solution on the web but no one succeed I even use to create details view and works fine on local but the same error after upload
i have tried
url: '/MobileS/Get/' + MobileSID
url: '~/MobileS/Get/' + MobileSID
url: '../MobileS/Get/' + MobileSID
and also i have tried despite its wrong but i think it may be work
url: '../../MobileS/Get/' + MobileSID
url: '~/~/MobileS/Get/' + MobileSID
url: 'MobileS/Get/' + MobileSID
this is the main function
function getbyID(MobileSID) {
$('#Name').css('border-color', 'lightgrey');
$.ajax({
url: '/MobileS/Get/' + MobileSID,
dataType: "json",
type: "GET",
contentType: 'application/json; charset=utf-8',
async: true,
processData: false,
cache: false,
success: function (data) {
//var obj = JSON.parse(data)
// alert(data);
$('#Name').val(data.MobileSerial);
$('#myModal').modal('show');
},
error: function (errormessage) {
alert(errormessage.responseText);
}
});
}
Hhi, i try to unlock my android keys with the phonegap WRITE API and more precisely with this method: https://build.phonegap.com/api/v1/keys/android/257852?auth_token=mytoken
This method returns :
{
"id":257852,
"title":"****",
"platform":"android",
"created_at":"2017-07-06 01:42:56 -0700",
"last_used":"2017-07-11 06:00:33 -0700",
"link":"/api/v1/keys/android/257852",
"default":false,
"alias":"****",
"locked":true
}
The problem is that suddenly my key is not unlock as you can see in the return and also on the web interface of build.
I do this request with javascript and jquery ajax :
$.ajax({
url: "https://build.phonegap.com/api/v1/keys/android/257852",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa("username:password"));
},
type: 'PUT',
dataType: 'json',
contentType: 'application/json',
processData: false,
data: '{"key_pw":"pass","keystore_pw":"pass"}',
success: function (data) {
alert(JSON.stringify(data));
},
error: function(){
alert("Cannot get data");
}
});
I do not understand what i do wrong...
Thank you for your help and sorry for my bad english !
EDIT :
Ok my bad.. i just give this to data field :
data: '{"key_pw":"pass","keystore_pw":"pass"}',
and API need something like that :
data: 'data={"key_pw":"pass","keystore_pw":"pass"}',
I have a javascript code that calls a web service from another server, it's working, but i need to call it from code behind due to an http call request issue, following my code :
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://10.8.111.12:7181/CatchEventLink.asmx/CollectDataLink",
data: JSON.stringify({ "pDataDictionnary": lDataCollected }),
dataType: "json",
async: true,
success: function (data, textStatus) {
if (textStatus == "success") {
//alert('success');
}
},
error: function (exception) {
//alert('Exeption : ' + exception);
}
});
Any suggestions please??
I'm trying to get a data from server without refreshing the page using ajax, So the problem the data come like a text not like json data
my code:
$.ajax({
type: "GET",
url: "http://localhost:8080/search?key=" + QUERY + "",
success: function (reslt) {
console.log(reslt);
console.log(reslt.length);
}
});
and the data on the server:
im using nodejs and express framework the code:
router.get('/search', function (req, res) {
tab = ['08:00', '09:00', '10:00', '11:00'];
res.end(JSON.stringify(tab));
});
why when i do console.log(reslt[3]); it's give me 8 , should give me 10:00
Use
dataType: 'json'
If your response is JSON, always set the datatype to json. Do it like this
$.ajax({
type: "GET",
dataType: 'json',
url: "http://localhost:8080/search?key=" + QUERY + "",
success: function (reslt) {
console.log(reslt);
console.log(reslt.length);
}
});
You have to use dataType and contentType attribute in your ajax request
$.ajax({
type: "GET",
dataType: 'json',
contentType: "application/json",
url: "http://localhost:8080/search?key=" + QUERY + "",
success: function (reslt) {
console.log(reslt);
console.log(reslt.length);
}
});
Below is my jquery ajax call. I see from fire bug that I am getting the json response.
Content-Type application/json
{x:1363590711.97,y:0.277528026651}
However...I cant event pop up and alert of the data? How do I get the parsed json object so i cna start working iwth it?
$.ajax({
type: 'GET',
url: 'ajax_test',
crossDomain: false,
dataType: 'json',
success: function(responseData) {
alert(responseData);
//seriesJsonData[0]['data'].push({y: responseData.y, x: responseData.x});
}
});
Your return data is already parsed when you request dataType: 'json'
$.ajax({
type: 'GET',
url: 'ajax_test',
crossDomain: false,
dataType: 'json',
success: function(responseData) {
alert(responseData.x + " " + responseData.y);
seriesJsonData[0]['data'].push(responseData);
}
});