I'm trying to fetch data from Youtube API using jQuery and Ajax GET request with the required parameter, including "part".
The GET request is ok (code 200), however when I see the data I'm getting an error 400 "required parameter: part".
Could anyone give me some tip to overcome this issue?
This is part of my jQuery and the result of the ajax request is right below.
$(document).ready(function(){
console.log('doc ok');
$('#tag-search').submit(function(){
var search_term = {
q: "test"
};
search(search_term);
});
});
function search(search_term) {
console.log('searching ...');
console.dir(search_term);
var search_term = '';
var desireSearch = $ ('#tag').val();
var channelName = 'Iron_Maiden';
$.ajax({
method: 'GET',
url: 'https://www.googleapis.com/youtube/v3/channels?',
part : 'contentDetails',
forUsername: channelName,
key: '(My personal key)',
dataType: 'jsonp'
})
.done(function(results){
var result = results.data;
console.log(result);
});
}
And this is what I got:
jQuery1900617815546458587_1453343296717 (
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Required parameter: part",
"locationType": "parameter",
"location": "part"
}
],
"code": 400,
"message": "Required parameter: part"
}
}
)
You need to use data: {} in the $.ajax request:
$.ajax({
method: 'GET',
url: 'https://www.googleapis.com/youtube/v3/channels?',
data: {
part : 'contentDetails',
forUsername: channelName,
key: '(My personal key)'
},
dataType: 'jsonp'
})
http://api.jquery.com/jquery.ajax/
Related
I am currently trying to get API responses from the two following API's. All of the sample code on their website is in PHP and asks for a token and token SSH, while they only give you an API key. Very lost trying to get requests to pull. The closest I've gotten is an error that says the following:
{status: "error", message: "Unable to read your data; it might not be in json format."}
here is my JS:
jQuery.get({
url: 'https://api.rescuegroups.org/http/v2.json',
type: 'post',
contentType: 'application/json',
data: {
apikey: 'XXXXXXX',
objectType:'animals',
},
dataType: 'json',
success: function (data) {
console.info(data);
}
});
Any help is greatly appreciated. Really want to avoid having to learn PHP as I'm still very new to JS.
the problem is that you are not passing it as json.
this is json format.
let dataObject = {
data: {
apiKey: "xxxx"
}
};
let data = JSON.stringify(dataObject);
{
"data": {
"apikey": "xxx"
}
}
then pass data as your data.
After trying it with Postman the request went through.
Obviously I don't have the api key
{
"status": "error",
"message": "Error with your login credentials",
"messages": {
"generalMessages": [
{
"messageID": "1101",
"messageCriticality": "error",
"messageText": "Error with your login credentials."
}
],
"recordMessages": []
}
}
Code
<!DOCTYPE html>
<html>
<head>
<title>JSSample</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(function() {
var params = {
// Request parameters
};
$.ajax({
url: "https://westus.api.cognitive.microsoft.com/linguistics/v1.0/analyzers?" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","fa5a4445a080414d95610f74fa3e5626");
},
type: "GET",
// Request body
data: "{body}",
})
.done(function(data) {
alert("success");
})
.fail(function() {
alert("error");
});
});
</script>
</body>
</html>
Documentation Help
https://westus.dev.cognitive.microsoft.com/docs/services/56ea598f778daf01942505ff/operations/56ea59bfca73071fd4b102ba
input json is below
{ "language" : "en", "analyzerIds" : ["4fa79af1-f22c-408d-98bb-b7d7aeef7f04", "22a6b758-420f-4745-8a3c-46835a67c0d2"], "text" : "Hi, Tom! How are you today?" }
required output is below json
[
{
"analyzerId" : "4fa79af1-f22c-408d-98bb-b7d7aeef7f04",
"result" : ["NNP",",","NNP","VBP","PRP","NN","."]
},
{
"analyzerId" : "22a6b758-420f-4745-8a3c-46835a67c0d2",
"result" : ["(TOP (S (NNP Hi) (, ,) (NNP Tom) (. !)))","(TOP (SBARQ (WHADVP (WRB How)) (SQ (VP (VBP are)) (NP (PRP you)) (NN today) (. ?))))"]
}
]
Looks like you're mixing up the 'List Analyzers' and 'Analyze Text' calls. Your input/output combination suggests you want the latter. If so, the code should be roughly as follows:
var request = {
"language": "en",
"analyzerIds": [
"4fa79af1-f22c-408d-98bb-b7d7aeef7f04",
"22a6b758-420f-4745-8a3c-46835a67c0d2"
],
"text": "Hi, Tom! How are you today?"
}
$.ajax({
url: "https://westus.api.cognitive.microsoft.com/linguistics/v1.0/analyze?" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","fa5a4445a080414d95610f74fa3e5626");
},
type: "POST",
// Request body
data: JSON.stringify(request),
})
.done(function(data) {
alert("success");
})
.fail(function() {
alert("error");
});
The four differences are:
The URL is /analyze, not /analyzers
The type is POST, not GET
The Content-Type needs to be specified
The data object needs to contain the request JSON.
Here is my code
<script type="text/javascript">
$(function() {
var params = {
// Request parameters
"returnFaceId": "true",
"returnFaceLandmarks": "false",
"returnFaceAttributes": "{string}",
};
$.ajax({
url: "https://westus.api.cognitive.microsoft.com/face/v1.0/detect?" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","8b99a9ed839a40a08aa8b529ef0f9b8c");
},
type: "POST",
// Request body
data: '{ "url": "http://heightline.com/wp-content/uploads/Tom-Cruise-smile.jpg" }'
})
.done(function(data) {
alert("success");
})
.fail(function() {
alert("error");
});
});
Here is the image of theres documentation about 400 responce from the server`enter when i enter or access the page then its shows error help how to resolve this
"returnFaceAttributes": "{string}" is making the error.
Actually it is the {string} value. It should be specified, i.e. age, gender ...
Try to replace "returnFaceAttributes": "{string}" with "returnFaceAttributes": "age" and it should work.
Check the documentation: https://www.microsoft.com/cognitive-services/en-us/face-api/documentation/glossary
Iam trying to use youtube api for finding the information about a particular video using youtube api.I have used the https module for sending and receiving data
This is the code I have used
var youtube_query=querystring.stringify({
q:'bangarang',
key:'api_key',
part:'snippet'
});
var options_you = {
host:'www.googleapis.com',
method:'GET',
path:'/youtube/v3/search'
};
function getvid_id(vid_result){
//callback function for finding the information on the video
vid_result.setEncoding('utf8');
console.log("STATUS :"+vid_result.statusCode);//to show the status code
vid_result.on('data', function (body) {
console.log(body);
});
}
var youtube_request = https.request(options_you,getvid_id);
youtube_request.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
youtube_request.write(youtube_query);
youtube_request.end();
However I get the following response
STATUS :400
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Required parameter: part",
"locationType": "parameter",
"location": "part"
}
],
"code": 400,
"message": "Required parameter: part"
}
}
As you can see ,I have already provided the part parameter.But I cannot find the reason why it is not working .
You can pass querystring directly to the path field as :
var querystring = require("querystring");
var https = require("https");
var youtube_query = querystring.stringify({
q: 'bangarang',
key: 'api_key',
part: 'snippet'
});
var options_you = {
host: 'www.googleapis.com',
method: 'GET',
path: '/youtube/v3/search?' + youtube_query
};
var youtube_request = https.request(options_you, function(res) {
res.on('data', function(d) {
process.stdout.write(d);
});
});
youtube_request.end();
I'm trying to build an app that gets JSON from server and then shows a javascript alert if the JSON response has designated row. The JSON I get from server looks like this:
{
"key": [
{
"IND": "406",
"NUMBER": "9",
"MESSAGE": "this is a test",
"status": "ok"
}
]
}
And this is the code I use to show the alert:
function UpdateRecord(update_id) {
var id = getUrlVars()["id"];
jQuery.ajax({
type: "POST",
url: serviceURL + "test.php",
data: 'id=' + id,
cache: false,
success: function(data) {
if (data.status == 'ok') {
alert(data.message);
} else {
alert("no");
}
}
});
}
But this code alerts "no" even though the JSON has a row "status": "ok"
Try with if (data.key[0].status), and replace alert(data.message) with alert(data.key[0].MESSAGE). You have to be careful with capitalization!
you have "key" defined in your jSON, sohould it not be
if(data.key[0].status == "ok")
Do a console.log(data) in the success handler to see what the data is. You will see that there is no data.status, but instead it would be data.key[0].status.