I am sending a POST request using JS for Micosoft QnA Maker API. But it is returning a JSON file with Error Resourse Not Found
{ "error": { "code": "ResourceNotFound", "message": "The requested resource was not found." } }
Although I am doing everything correct according to the API still getting the same error.
I am using the code in JS as shown below:
<script type="text/javascript">
$(function() {
var params = {
"question": "is qna maker free?",
"top": 3
};
$.ajax({
url: "https://westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/XXXX/generateAnswer?" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","XXXX");
},
type: "POST",
// Request body
data: "{body}",
})
.done(function(data) {
alert("success");
})
.fail(function() {
alert("error");
});
});
This code is returning alert("error") and also showing the following message in console:
westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/XXXX/generateAnswer?question=is+qna+maker+free%3F&top=3:1 POST https://westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/XXXX/generateAnswer?question=is+qna+maker+free%3F&top=3 400 (Bad Request)
I am referring to this link
I have seen that there Response 400 says that "Argument question is not specified." But I have specified the question.
What's wrong with my code? I am quite sure that there's must be something wrong with this in my code.
"question": "is qna maker free?",
"top": 3
When I visit this link
It also shows the same JSON file with the error.
you have in your code:
$.ajax({
url: "https://westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/XXXX/generateAnswer?" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","XXXX");
},
type: "POST",
// Request body
data: params, //replace {body} with the params variable
dataType: "json" //add this line
})
You might also want to handle the response with success/error functions so it will look like this:
$.ajax({
url: "https://westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/XXXX/generateAnswer?" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","XXXX");
},
type: "POST",
data: params,
dataType: "json",
success: function(data){
alert("success");
},
error: function(e){
console.log(e);
}
})
Related
I'm dealing with the todoist API (https://developer.todoist.com/) and I am making a jquery ajax get request for some data with this:
var url = "https://todoist.com/API/v7/sync";
var data = {
'token' : token,
'resource_types' : '["all"]',
};
$.ajax({
url: url,
data: data,
type: 'GET',
dataType: 'jsonp',
success: function(response) {
console.log(response);
},
error: function(response) {
console.log('error');
},
});
Now, when I get the response, I get the error
Unexpected token :
Why? Because according to (https://stackoverflow.com/a/7941973/2724978) jQuery is expecting a jsonp formatted response, but it returns json.
I've researched all over for how to solve this, and the response would be: "Return the data in jsonp format".. well. It's an external API and they don't provide data in JSONP. Is there a way I could override the returned function and parse this JSON data anyway?
Your dataType should be json, not jsonp.
As elektronik pointed out the dataType should be json and not jsonp. The code than looks as following ...
var token = "your token"
var url = "https://todoist.com/API/v7/sync";
var data = {
'token' : token,
'resource_types' : '["all"]',
};
jQuery.ajax({
url: url,
data: data,
type: 'GET',
dataType: 'json',
success: function(response) {
console.log(response);
},
error: function(response) {
console.log('error');
},
});
I'm trying to use the postmates API, which first requires us to authenticate ourselves using http basic authentication. The username field in the code below is where we inserted our private API key.
<script>
$(document).ready(function(){
// Request with custom header
$.ajax({
url: ' http://username:#api.postmates.com',
type: 'GET',
dataType: 'jsonp',
success: function(response) { alert("Success"); },
error: function(error) {alert(error); }
});
});
</script>
The error we are getting is
XMLHttpRequest cannot load
http://api.postmates.com/?callback=jQuery112008309037607698633_1462052396724&_=1462052396725.
Response for preflight is invalid (redirect)
need the authentication
https://postmates.com/developer/docs#authentication
The actual header that is used will be a base64-encoded string like
this:
Basic Y2YyZjJkNmQtYTMxNC00NGE4LWI2MDAtNTA1M2MwYWYzMTY1Og==
Try to
$(document).ready(function(){
// Request with custom header
$.ajax({
url: ' http://username:#api.postmates.com',
type: 'GET',
dataType: 'jsonp',
success: function(response) { alert("Success"); },
error: function(error) {alert(error); },
beforeSend: function (xhr) {
xhr.setRequestHeader ("Authorization", "Basic Y2YyZjJkNmQtYTMxNC00NGE4LWI2MDAtNTA1M2MwYWYzMTY1Og==");
}
});
});
I don't test because jsfiddle block external petitions.
Before asking my question , i try several method, google it my issue and decided to post here my problem.
I use imgur api 3 with javascript, i have a function to make favorite an image but without success.
Here some sample of what i did
with my client id:
$.ajax({
type: "POST",
url: 'https://api.imgur.com/3/image/'+ currentId +'/favorite',
//headers:{
// 'Authorization':'Client-ID Client-id'
//},
success: function(data) {
console.log(data);
},error:function(error,a,b){
console.log(error,a,b);
}
});
Result : permission denied, 403
with my access_token:
$.ajax({
type: "POST",
url: 'https://api.imgur.com/3/image/'+ currentId +'/favorite',
headers:{
'Authorization ': 'Bearer '+params.access_token
},
success: function(data) {
console.log(data);
},error:function(error,a,b){
console.log(error,a,b);
}
});
Result : "SyntaxError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'Authorization ' is not a valid HTTP header field name."
And without the headers i have : "Authentication required", 401
here the link for favorite an image
Thanks in advance for your help.
I have the following code:
$("form").submit(function()
{
//Checking data here:
$("input").each(function(i, obj)
{
});
alert(JSON.stringify($(this).serializeArray()));
var url='http://127.0.0.1:1337/receive';
$.ajax({
url: url,
type: 'POST',
contentType:'application/json',
data: JSON.stringify($(this).serializeArray()),
dataType:'json'
});
});
And after I submit the form, I get a JavaScript alert with the json string, so that is made correct (on my server I only log it so it does not matter what it is in it). If I try to send a request to the same link from postman it works, it logs it.
I think I'm doing something wrong in the ajax call, but I am not able to figure out.
Try below piece of code. Add success and error handler for more details
$("form").submit(function()
{
//Checking data here:
$("input").each(function(i, obj)
{
});
alert(JSON.stringify($(this).serializeArray()));
var url='http://127.0.0.1:1337/receive';
$.ajax({
url: url,
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify($(this).serializeArray()),
dataType:'json',
success : function(response) {
alert("success");
},
error: function (xhr, status, error) {
alert(error);
}
});
});
data:{ list : JSON.stringify($(this).serializeArray())}
From the Jquery docs:
Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.
crossDomain attribute simply force the request to be cross-domain. dataType is jsonp and there is a parameter added to the url.
$.ajax({
url:'http://127.0.0.1:1337/receive',
data:{ apikey: 'secret-key-or-any-other-parameter-in-json-format' },
dataType:'jsonp',
crossDomain: 'true',
success:function (data) {alert(data.first_name);}
});
I have client URL and getting response from that URL through browser. While sending through AJAX I am getting a null response. Right now I am working with a .Net application. Here I am given my script. Please guide me to get proper response and thanks in advance.
Response:
{
"resultFlag": false,
"message": "Dealer not found",
"info": []
}
$.ajax({
type: "GET", //GET or POST or PUT or DELETE verb
url: URL,
//data: data,
dataType: "json",
contentType: "application/json; charset=utf-8", // content type sent to server
success: function (result) {
// JSON.stringify(result);
alert(JSON.stringify(result));
},
error: function () {
alert('error');
}
});