I have an AJAX request which aims at my API but the response goes in to the error callback instead of the success callback and I don't know why. The status response header is the success status (200). Can you help me please?
I can avoid this issue with the complete callback and if the status code is 200 or 400 I will display what I want, but it will not solve the real problem. It's actually what I did but it's not the best solution.
$.ajax({
url: 'http://api.asaplace.v-labs.fr/users/me/cards/' + numCard,
dataType: 'json',
type: 'DELETE',
headers: {
'Authorization': 'Bearer ' + this.state.token
},
success: function() {
console.log("success");
},
complete: function(xhr) {
alert("carte supprimée avec succès");
console.log("error");
},
});
The request
enter image description here
I found the error the "dataType" was in JSON I put it in xml and it works thank's all for your help
Related
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);
}
})
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 am using jQuery's ajax method to submit an ajax request like this:
$.ajax({
type: "PUT",
url: someURL,
contentType: "application/json",
data: JSON.stringify(data),
dataType: "json"
})
How can I add error handlers for the http status codes 401 and 404?
From the jQuery documentation, you can implement handlers for specific status code responses as options when calling .ajax():
$.ajax({
type: "PUT",
url: someURL,
contentType: "application/json",
data: JSON.stringify(data),
dataType: "json",
statusCode: {
404: function() {
// handle the 404 response
},
401: function() {
// handle the 401 response
}
}
})
Look at the docs for goodness sake.
statusCode (default: {})
Type: PlainObject
An object of numeric HTTP codes and functions to be called when the response has the corresponding code. For example, the following will alert when the response status is a 404:
$.ajax({
statusCode: {
404: function() {
alert( "page not found" );
}
}
});
If the request is successful, the status code functions take the same parameters as the success callback; if it results in an error (including 3xx redirect), they take the same parameters as the error callback.
(version added: 1.5)
It's really simple to find an answer if you look at the documentation that describes the behavior of a method.
I am attempting to integrate the Instapaper Simple API into something but I am struggling to understand how to handle the response that the API sends back in Javascript. The article is adding to Instapaper just fine so I know that the submission is working just not my response handlers.
This is the code I have so far and I'm guessing that the success function is not the correct way of handling the response.
$.ajax({
type: 'GET',
url: url,
dataType: 'jsonp',
success: function( data, status ) {
alert("yay");
},
error: function(status) {
alert("oh noes");
}
});
return false;
Instapaper returns a 201 when the article has been added. I can see that in the Google Chrome Network tool that the GET returned a 201 status. Just wondering how I handle that status within the code above.
Thanks.
Edit
When I click the link to activate the code below it pops up the alter under the error function right now even though it has worked.
jQuery.ajax() provides statusCode map for such purposes:
$.ajax({
type: 'GET',
url: url,
dataType: 'jsonp',
statusCode: {
200: function( data ) {
alert("yay");
},
201: function( data ) {
}
},
error: function(status) {
alert("oh noes");
}
});
http://api.jquery.com/jQuery.ajax/
$.ajax({
statusCode: {
201: function() {
alert("201!");
}
}
});
this should work with any http status code
I am trying to post a request to the google authSub for youtube. I use jQuery for AJAX posts etc. When I make the POST I get a 405 error: "405 Method Not Allowed".
Here is my js:
$.ajax({
type: 'POST',
url: "https://www.google.com/accounts/AuthSubRequest",
beforeSend: function(xhr){
xhr.setRequestHeader('X-GData-Key', 'key="' + ytKey + '"');
},
success: function(oData){
//alert(oData);
},
scope: 'http://gdata.youtube.com',
alt: 'json',
next: 'http://' + globalBrand + '/sitecreator/view.do',
session: 1
});
The page I am using in the API for this is here.
Here is what the error looks like:
Your data parameters for the request are misplaced see below:
$.ajax({
type: 'POST',
url: "https://www.google.com/accounts/AuthSubRequest",
data: {
scope: 'http://gdata.youtube.com',
alt: 'json',
next: 'http://' + globalBrand + '/sitecreator/view.do',
session: 1
},
beforeSend: function(xhr){
xhr.setRequestHeader('X-GData-Key', 'key="' + ytKey + '"');
},
success: function(oData){
//alert(oData);
}
});
Now it may be something youre doing wrong additionally, but this for sure needs to be corrected.
Ah, here is a solution to the problem. If I make the request with the url built out and assigned as an href to an anchor or call it in window.open(); it works.
window.open('https://www.google.com/accounts/AuthSubRequest?scope=http://gdata.youtube.com&next=http://' + globalBrand + '/sitecreator/view.do&session=1');
As for why jQuery's method with the ajax was being rejected I know not. It seems to be an issue elsewhere also.
Solution to error HTTP HEADER OPTIONS in JQuery, this request is ok for me:
var word = 'search+word';
$.ajax({
type: "GET",
url: "http://gdata.youtube.com/feeds/api/videos?q="+word+"&max-results=10&alt=json-in-script&format=5,
cache: false,
dataType:'jsonp',
success: function(data){
//json 'data' var treatment
},
error: function(XMLHttpRequest, textStatus, errorThrown, data){
alert("Not able to fetch the data due to feed unavailability!!!");
}
});
Reference: http://www.mohammedarif.com/?p=180