Hey guys im trying to send a push message to my phone using
the pushover libary (pushover.net).
Actually I failed pretty fast in fact Im getting a 400 Bad request error
from jquery AJAX call.
Here is my code, whats wrong ?
$.ajax({
type: "POST",
url: "https://api.pushover.net/1/messages.json",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: {
"title": title,
"message": message,
"token": CONFIG.API,
"device": CONFIG.DEVICE,
"user": CONFIG.USER,
},
complete: function(data) {
console.log("push sent!");
console.log(data);
}
});
Error:
POST https://api.pushover.net/1/messages.json 400 (Bad Request)
Related
I am making a project for local use using jQuery and Node.js + Express API.
When I "place an order" using Postman there is no problem:
When I do, what seems to me, the same with jQuery, I get an internal server error 500:
$.ajax({
type: "post",
url: "http://localhost:3001/roomorder",
data: {
roomOrderLines: global_orderSummary,
},
dataType: "json",
success: (placeOrderResult) => {
$('#appContent').html(`success!`);
},
error: (xhr, ajaxOptions, thrownError) => {
global_setError("An error occurred. / "+xhr.status+" / "+thrownError);
}
});
the global_orderSummary looks like this when I console.log it, looks identical to me as what I use as data in Postman:
Any help would be much appreciated!
I had to make these adjustments, now it works:
type: "post",
url: "http://localhost:3001/roomorder",
data: JSON.stringify ({roomOrderLines: global_orderSummary}),
dataType: "json",
contentType: "application/json",
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);
}
})
I already created MVC spring and I want consume with SAPUI5 (javascript) with AJAX but I found an error "415 (Unsupported Media Type)". I use swagger in spring for test CRUD. in swagger, I success for insert data but failed in AJAX.
controller Spring:
#PostMapping(value={"/tesinsert"}, consumes={"application/json"})
#ResponseStatus(HttpStatus.CREATED)
public ResponseEntity<?> insert(#RequestBody KasusEntity user) throws Exception {
Map result = new HashMap();
userService.insertTabel(user);
return new ResponseEntity<>(result, HttpStatus.CREATED);
}
in javascript:
var data = {
"kodekasus":5,
"nama":"baru",
"isdelete":1,
"createdby":"hahaa",
"createddate":null,
"updatedby":"hihii",
"updateddate":null
};
$.ajax({
type: 'POST',
url: url,
data: data,
success: function(data) {
console.log('sukses: '+data);
},
error: function(error){
console.log('gagal: '+error);
}
});
if I code above in AJAX, show error "415 (Unsupported Media Type)", if I add in AJAX show different error: "Response for preflight has invalid HTTP status code 403
":
headers: {
Accept : "application/json; charset=utf-8",
"Content-Type": "application/json; charset=utf-8"
}
How to solve this problem?
Thanks.
Bobby
Add dataType: 'json' in your ajax call:
$.ajax({
type: 'POST',
url: url,
data: data,
dataType: 'json',
success: function(data) {
console.log('sukses: '+data);
},
error: function(error){
console.log('gagal: '+error);
}
});
I'm using a jquery ajax for storing a data in the database. I call a service through a url and send data to it in the following way:
$.ajax({
type: "POST",
url: "http://192.168.250.118:8080/rest_service/rest/user",
data: JSON.stringify({ "loginName": "tsample#gmail.com", "mobile": "1234567890" }),
async: false,
contentType: "application/json",
crossDomain: true,
success: function(data){
alert("success");
},
error: function(XMLHttpRequest, textStatus, errorThrown){
console.log(XMLHttpRequest);
console.log(textStatus);
console.log(errorThrown);
}
});
When I execute this, I get this error:
{"readyState":0,"status":0,"statusText":"error"}
This is what I get when I look for errors in network:
But, if I try using postman, the data is getting stored and I get 200OK response. What's wrong with the above code? What should I change?
Stringify the data before sending - data: JSON.stringify({ "loginName": "tsample#gmail.com", "mobile": "1234567890" }) amd contentType:"application/json"
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');
}
});