I get a problem with ajax get request can someone help me? - javascript

My code do not response any data to me..
bellow some code:
<script type="text/javascript">
function matriculaFn(mat) {
$.ajax({
method: 'GET',
url:'My url API,
async: true,
crossDomain: false,
contentType: 'application/json;charset=utf-8',
dataType: 'json',
headers: {
"Authorization": "Token xxxprivatexxxxx"
},
options: {
useSSL: true
},
success: function(data) {
console.log(data);
var dados = JSON.stringify(data);
console.log(dados);
$("#IDDOCAMPO").val(data.AlgumaCoisa);
},
//error : function(xhr, status, error) {
// do stuff here
//var data = jQuery.getJSON(xhr.responseText);
//console.log(data);
//}
});
};
</script>
here is the error:
jquery-3.3.1.min.js:2 OPTIONS https://url net::ERR_CERT_DATE_INVALID

Can't comment yet because I don't have enough rep but it looks as though this error is Chrome specific. Have you tried other browsers? If so, try some of the methods listed here: link
Other things you could try are:
crossDomain: true,
or adding:
xhrFields: {
withCredentials: true
},
or because it's a GET request try:
dataType: 'jsonp',

Related

JSON Multiple urls with the headers

I am trying to have multiple urls in $.ajax https://api.twitch.tv/kraken/channels/ and https://api.twitch.tv/kraken/streams/
$.ajax({
type: 'GET',
url: 'https://api.twitch.tv/kraken/channels/' + item,
headers:{
'Client-ID': 'k7uj51l1cteh0sbhwplk4hqq6c7bqo'},
success: function(data){
console.log(data);
},
error: function(data){
alert("doesn't work")
},
});
but I also need to have headers with 'Client-ID'. I was trying with this:
$.getJSON(channel, function(e){
$.getJSON(stream,function(f){
});
});
but it doesn't work.
it seems you forgot to add one more header "accept": "application/vnd.twitchtv.v5+json"
The complete working code example
var settings = {
"async": true,
"crossDomain": true,
"url": "https://api.twitch.tv/kraken/channels/44322889",
"method": "GET",
"headers": {
"client-id": "k7uj51l1cteh0sbhwplk4hqq6c7bqo",
"accept": "application/vnd.twitchtv.v5+json"
}
}
$.ajax(settings).done(function (response) {
console.log(response);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
checkout this code

getting data but unable to save it ajax

This is my code for a service
function usersClicked() {
var response;
$.ajax({
url: 'somedomain.com/get_users',
type: 'GET',
dataType: 'jsonp',
crossDomain: true,
contentType: "application/json; charset=utf-8",
success: function (data, status, xhr) {
alert(data);
}
});
}
In network tab, I am able to see the response but it is unable to alert data. No dialog is being shown. can anyone help me with this
Response Header

parse error when trying get request with ajax?

I have seen the previous questions asked by others in stack overflow.but did'nt work for me.Iam getting the data in json when seen in the network panel,but
var jsonData = {
"name": fbUsername,
"email": fbEmail
};
console.log(jsonData);
$.ajax({
type: "get",
contentType: "application/json",
data: jsonData,
jsonp: false,
jsonCallback: 'jsonCallback',
dataType: 'jsonp',
crossDomain: true,
headers: { "api_key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" },
url: "https://xaxxxxxxxxxx/prod/users",
success: function(res) {
alert("success!");
},
error: function(jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
});
function jsonCallback(results){
alert(results);
}
PARSE ERROR
I am getting the response in JSON
['name1','name2']
although the response is JSON and why it is coming an parse error?

JSON ajax post data - get 400 bad request message

First of all. I read many questions about this problem here in stackoverflow but nothing helps me.
I've this function:
function ip_login(){
//alert(JSON.stringify(arr_login));
$.ajax({
crossdomain: true,
url: 'https://****/Token',
type: 'POST',
data: JSON.stringify(arr_login),
contentType: 'application/x-www-form-urlencoded',
dataType: 'json',
success: function (data) {
console.log(data);
}
});
}
And this is my JSON string:
{"grant_type":"password","username":"mail#mail.de","password":"blabla"}
With the google chrome extension 'postman' it works fine!
If I test my code above I got the popular 400 bad request message.
When I take the code of the extension 'postman' it doesn't work too.
This is the code of postman:
var settings = {
"async": true,
"crossDomain": true,
"url": "https://*****/Token",
"method": "POST",
"headers": {
"cache-control": "no-cache",
"content-type": "application/x-www-form-urlencoded"
},
"data": {
"grant_type": "password",
"username": "mail#mail.de",
"password": "blabla"
}
}
$.ajax(settings).done(function (response) {
console.log(response);
});
I can't fine my issue..
I searched on Google and SO but no reply helped me.
Edit:
This is in my console:
"NetworkError: 400 Bad Request - https://awesome-url.de/Token"
The problem is in stringifiing JSON. Server expects json but you send string(stringified JSON). Try
function ip_login(){
//alert(JSON.stringify(arr_login));
$.ajax({
crossdomain: true,
url: 'https://****/Token',
type: 'POST',
data: arr_login,
contentType: 'application/x-www-form-urlencoded',
dataType: 'json',
success: function (data) {
console.log(data);
}
});
}
Not sure if this helps, but try anyways.
When I'm sending Json, I didn't use "JSON.stringify(arr_login)"
All I did was something like "data:{arr_login:arr_login}"
This is how my variable looks like.
var req = new Object();
req["type"] = 'refresh';
req["time"] = window.page_ordertime;
This is how my ajax looks like
$.ajax( {
url:"listorder_process.php",
method: "POST",
dataType:"json",
data:{request:req}
} )

AJAX post is empty on the server side (PHP)

I am making an AJAX request to a PHP controller, by using jQuery ajax, but when trying to get the posted data with PHP the $_POST is empty. Below is the actual function:
function GetSeriesForManufacturer(manuf) {
selectedmanufacturer = manuf;
//Make an AJax Call For Getting Series Of Manufacturer
var series = null;
$.ajax({
type: "POST",
url: url,
data: "{manufacturer:'" + selectedmanufacturer + "'}",
contentType: "application/json", //; charset=utf-8",
dataType: "json",
cache: false,
async: false,
success: function (response) {
//remove loading gif
$(".loading").hide();
//Append Data
AppendSeries($.parseJSON(response.text), selectedmanufacturer);
//Custom Scrollbar Call
$('.MatchingSeries ul').mCustomScrollbar();
},
error: function (XMLHttpRequest, textStatus, errorThrown) { }
});
}
Thanks in advance!
First, you don't need to stringify data. Just send object literal is ok.
data: {manufacturer: selectedmanufacturer},
Second, you don't need this line:
contentType: "application/json",
Let jQuery do the encoding for you:
$.ajax({
type: "POST",
url: url,
data: {
manufacturer: selectedmanufacturer
},
contentType: "application/json", //; charset=utf-8",
dataType: "json",
cache: false,
async: false,
success: function (response) {
...
},
error: function (XMLHttpRequest, textStatus, errorThrown) { }});

Categories