JSON Multiple urls with the headers - javascript

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

Related

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

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',

How to get the Excel-File id using the filename in microsoft-graph api?

I am trying to get the File Id using the filename in graph API
I have tried the following thing but this is not working
https://graph.microsoft.com/v1.0/sites/ccc.sharepoint.com,dddddd,eeeeeeeeeeeeeeee/drive/root:/excelDir/filename.xlsx:/children
Got the answer
https://graph.microsoft.com/v1.0/sites/"+webAndSiteId+"/drives/"+folderID+"/root/search(q='filename.xlsx')
Below is the working javascript
var fileCollectionEndpoint = "https://graph.microsoft.com/v1.0/sites/"+webAndSiteId+"/drives/"+folderID+"/root/search(q='"+fileNamesArray[index]+"')"
$.ajax({
url: fileCollectionEndpoint,
async: false,
dataType: 'json',
type: "GET",
cache:false,
headers: {
'Authorization':'Bearer '+token,
},
success: function (json) {
console.log(json.value[0].id);
},
error:function(xhr)
{
}
});

Uncaught Reference:$ is not defined

I'm new to coding...just wanted to know what am i doing wrong in this
<script type="text/javascript">
// This example loads the "Canadian Parliament 2012"
// dataset from a CSV instead of from JSON. // System.import('app').catch(function (err) { console.error(err); });
$(function() {
Dimensions = "sector_type";
Measures = "";
tblname = "sample";
$.ajax({
method: "GET",
url: "http://localhost:5000/api/values/5"
headers: {
'Content-Type': 'application/json'
},
traditional: true,
async: false,
}).success(function results(data) {
chartdata = data.data;
alert("SUCCESS");
});
});
</script>
This is giving me Uncaught Reference :$ is not defined.Also is this the right way of writing a small script for retrieving data from localhost:5000 and what should i do to display the data
Any Help would be deeply appreciated
It's worked for me.
You should put the reference jQuery link.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
Then replace the code
<script type="text/javascript">
$(document).ready(function(){
Dimensions = "sector_type";
Measures = "";
tblname = "sample";
data = $(this).serialize();
$.ajax({
method: "GET",
url: "http://localhost:5000/api/values/5",
headers: {
'Content-Type': 'application/json'
},
traditional: true,
async: false,
success:function(data){
chartdata = data.data;
alert("SUCCESS");
}
});
});
</script>
Try my code.. I think it will work fine
(function () {
Dimensions = "sector_type";
Measures = "";
tblname = "sample";
$.ajax({
method: "GET",
url: "http://localhost:5000/api/values/5",
headers: {
'Content-Type': 'application/json'
},
traditional: true,
async: false
}).success(function results(data) {
chartdata = data.data;
alert("SUCCESS");
});
})();
check whether you've included the jquery library in your html code.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
$ is not defined means you have not added jQuery plugin in your page.
Download jQuery plugin from jquery.com and add in your project. Reference that file in your page header.
For displaying data, it depend on what data you are retrieving. According to return data add control and bind.
$(function() {
Dimensions = "sector_type";
Measures = "";
tblname = "sample";
$.ajax({
method: "GET",
url: "http://localhost:5000/api/values/5"
headers: {
'Content-Type': 'application/json'
},
traditional: true,
async: false,
success:function (data) {
chartdata = data.data;
alert("SUCCESS");
}
});
});
Use $(document).ready(function(){});
<script type="text/javascript">
// This example loads the "Canadian Parliament 2012"
// dataset from a CSV instead of from JSON. // System.import('app').catch(function (err) { console.error(err); });
$(document).ready(function() {
Dimensions = "sector_type";
Measures = "";
tblname = "sample";
$.ajax({
method: "GET",
url: "http://localhost:5000/api/values/5"
headers: {
'Content-Type': 'application/json'
},
traditional: true,
async: false,
}).success(function results(data) {
chartdata = data.data;
alert("SUCCESS");
});
});
</script>

how to get the json string in a variable

i got my json string inside the ajax as function like this way
$.ajax({
type: "POST",
url: "http://localhost/./Service/GetPageInfo",
dataType: "json",
contentType: 'application/json',
data: JSON.stringify({
filename: filename
}),
success: function (data) {
alert('Success');
},
error: function () {
alert('Error');
}
});
here i get data like
[{"main":{"sub":[],"tittle":"manu","startvalue":"","stopvalue":"","status":"","accumalated":"","comment":""}}]
i want it in a variable like
var myjsonobject =[{"main":{"sub":[],"tittle":"manu","startvalue":"","stopvalue":"","status":"","accumalated":"","comment":""}}]
There you go :
$.ajax({
type: "POST",
url: "http://localhost/./Service/GetPageInfo",
dataType: "json",
contentType: 'application/json',
data: JSON.stringify({
filename: filename
}),
success: function (data) {
alert('Success');
var jsonobject = data;
},
error: function () {
alert('Error');
}
});
Also I strongly advise you to use promises to make API calls: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Promise
var jsonobject= null;
$.ajax({
type: "POST",
url: "http://localhost/./Service/GetPageInfo",
dataType: "json",
contentType: 'application/json',
data: JSON.stringify({
filename: filename
}),
success: function (data) {
jsonobject=data;
alert('Success');
},
error: function () {
alert('Error');
}
});
If you want wait for ajax response and fill up variable then pass async: false in ajax request options.
Based on your comment, you need to parse the JSON in your success handler,
success: function (data) {
alert('Success');
var myjsonobject = JSON.parse( data );
},

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}
} )

Categories