I didn't get required input and output on Azure Cognitive services - javascript

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.

Related

Pass parameters in body to post in AJAX

I am facing issue posting values as parameters with jQuery post method with the script as per below:
<script type="text/javascript">
$(function() {
var params = {
// Request parameters
};
$.ajax({
url: "https://someurl.com/bravsoap.asmx?op=ptr" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","text/xml");
},
type: "POST",
// Request body
data: "{body}",
})
.done(function(data) {
alert("success");
})
.fail(function() {
alert("error");
});
});
</script>
This is sample from API vendor:
<ptr_PostToRoom xmlns="http://someurl.com">
<SessionID>9283-243342-422323423-2343e</SessionID>
<PostingM>PostingMethod_RoomID</PostingM>
<PostingP>002</PostingP>
<Header>
<Code>FOOD</Code>
<Description>Crystal Room – Beverage – Order No. 12345</Description>
<Quantity>1</Quantity>
</Header>
<Transactions>
<Code>FOOD</Code>
<Description>Dinner</Description>
<Quantity>1</Quantity>
<GrossTotal>35.50</GrossTotal>
<Code>BARDRINK</Code>
<Description>Bar Drinks</Description>
<Quantity>1</Quantity>
<GrossTotal>12.75</GrossTotal>
</ptr_PostToRoom>
Sorry for the bad explanation in first place.

Cannot rewrite some html content with ajax

I wanna rewrite my HTML content with ajax. A part can be rewritten, but not for others. This is my js :
$.ajax({
url: 'http://localhost/ThePrpuz/public/mahasiswa/getDetail',
data: {
nim: nim
},
method: 'post',
dataType: 'json',
success: function (data) {
console.log(data);
if ((data.gender) == 'Wanita') {
$('#foto').attr('src', 'http://localhost/theprpuz/public/img/cewek.jpg');
$('#foto').attr('alt', 'Cewek');
} else {
$('#foto').attr('src', 'http://localhost/theprpuz/public/img/cowok.jpg');
$('#foto').attr('alt', 'Cowok');
}
$('#nama').html(data.nama);
$('#nim').html(data.nim);
$('#univ').html(data.univ);
$('#fakultas').html(data.fakultas);
$('#jurusan').html(data.jurusan);
$('#penjurusan').html(data.penjurusan);
$('#ttl').html(data.tempat.concat(', ', data.tanggal));
}
});
The problem is, content with ** ID: ttl** can be rewritten. But did not for others. The result like this :
Nama :
Nim :
TTl : Somewhere, sometime
Univ :
Fakultas :
Jurusan :
Penjurusan :
Ih this case, I use console.log(data) to see that my ajax works or not. and in console, these are shown :
{
"nama": "Muhammad Fachri Saragih",
"tanggal": "2001-09-11",
"tempat": "Sibolga",
"gender": "Pria",
"univ": "Sriwijaya",
"fakultas": "Ilmu Komputer",
"jurusan": "Sistem Komputer",
"penjurusan": "Jaringan",
"nim": "9011281924069"
}
The method concat() is used to join two or more arrays, not strings!
Replace
$('#ttl').html(data.tempat.concat(', ', data.tanggal));
with
$('#ttl').html(data.tempat + ', ' + data.tanggal);
First, try to check the data type of the "data". I think it can't be accessed because the data type is still string, not JSON.
try to parse data to JSON.
$.ajax({
url: 'http://localhost/ThePrpuz/public/mahasiswa/getDetail',
data: {
nim: nim
},
method: 'post',
dataType: 'json',
success: function (data) {
console.log(data);
console.log(typeof data);
var obj = JSON.parse(data);
if ((obj.gender) == 'Wanita') {
$('#foto').attr('src', 'http://localhost/theprpuz/public/img/cewek.jpg');
$('#foto').attr('alt', 'Cewek');
} else {
$('#foto').attr('src', 'http://localhost/theprpuz/public/img/cowok.jpg');
$('#foto').attr('alt', 'Cowok');
}
$('#nama').html(obj.nama);
$('#nim').html(obj.nim);
$('#univ').html(obj.univ);
$('#fakultas').html(obj.fakultas);
$('#jurusan').html(obj.jurusan);
$('#penjurusan').html(obj.penjurusan);
$('#ttl').html(obj.tempat.concat(', ', obj.tanggal));
}
});

How to receive judgement result from model of Custom Vision with JavaScript

I want to receive judgement result from model of Azure Custom Vision by using JavaScript.
I changed JavaScript code that this site has.
https://southcentralus.dev.cognitive.microsoft.com/docs/services/eb68250e4e954d9bae0c2650db79c653/operations/58acd3c1ef062f0344a42814
But I can't.
What is wrong with my code?
I changed IterationId, application, url, content-Type, Prediction-key, and data.
These part are enclosed with {} in the code below.
<!DOCTYPE html>
<html>
<head>
<title>Human</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
"iterationId": "{Iteration id that showed in Performance Page}",
"application": "{My Project name of Custom Vision}",
};
$.ajax({
url: "{url that showed in "How to use the Prediction API"}" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/octet-stream");
xhrObj.setRequestHeader("Prediction-key","{my prediction key that showed in "How to use the Prediction API"}");
},
type: "POST",
// Request body
data: "D:\some name\some name\image.jpg",
})
.done(function(data) {
alert("success");
})
.fail(function() {
alert("error");
});
});
</script>
</body>
</html>
Of course, I expected showing "success".
But, the actual output is "error"......
When I changed URL that this site has(https://southcentralus.dev.cognitive.microsoft.com/docs/services/eb68250e4e954d9bae0c2650db79c653/operations/58acd3c1ef062f0344a42814) in my code, I can get Success message.
And, I also write
processData: false,
contentType: false,
in ajax in my code
Change your code to see what is the error that you get back:
(Note the new "error" parameter to the request)
$.ajax({
url: "{url that showed in "How to use the Prediction API"}" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/octet-stream");
xhrObj.setRequestHeader("Prediction-key","{my prediction key that showed in "How to use the Prediction API"}");
},
type: "POST",
// Request body
data: "D:\some name\some name\image.jpg",
error: function(xhr,status,error) {
// >>>>>>>>>>>> CHECK HERE THE ERROR <<<<<<<<<<<<
}
})
.done(function(data) {
alert("success");
})
.fail(function() {
alert("error");
});
Once you have the error, it would be easier to help you.

Using Microsoft face api its object return 400 that image is invalid or argument is invalid

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

Project Oxford - HTTP 404 when calling Computer Vision API through Javascript

I am trying to get JSON data from Microoft Project Oxford through a API call. I followed the API reference but when I make a call I get a 404 error.
<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
"visualFeatures": "All",
};
$.ajax({
url: "https://api.projectoxford.ai/vision/v1/analyses&" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","0000000000000000");
},
type: "POST",
// Request body
data: '{ "Url": "http://www.sweetheartmotors.ca/sites/default/files/audi_PNG1736.png" }',
})
.done(function(data) {
alert("success");
//display data
console(data);
})
.fail(function() {
alert("error");
});
});
</script>
What is stopping me from making the call?
You need to change the URL to end in a question mark, rather than an ampersand: https://api.projectoxford.ai/vision/v1/analyses?
Unfortunately most of the samples on the projectoxford.ai site contain this error.

Categories

Resources