I have my API which accepts Request Param:
#PostMapping(value = "/export")
#ResponseBody
public ResponseEntity<String> bulkExport(
#RequestParam(value = "managedObjects", required = false) List<String> managedObjects) {
//data
}
);
I want to send AJAX POST request.
$.ajax({
type: "POST",
//url: "policy/js_policy",
url: "/export/ ,
async: false,
data: { "managedObjects": ["Audit","Logs"]},
contentType: "application/json; charset=utf-8",
dataType: "json",
complete: function (XMLHttpRequest, textStatus) {
//File Handling
}
});
I tried to send managedObjects in URL. In data also I am sending the same.But my API is not working. How to send the #RequestParam from AJAX POST request exactly?
pass a list in Query Param
$.ajax({
...
url: "/export?managedObjects=Audit,Logs" ,
...
});
pass a list in Request Body
$.ajax({
type: "POST",
url: "/export/",
...
data: {managedObjects[0]: "Audit",
managedObjects[1]: "Logs"}
...
});
Try stringifying your data:
var data = {
managedObjects: ["Audit", "Logs"]
}
$.ajax({
type: "POST",
url: "/export/",
async: false,
data: JSON.Stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
complete: function (XMLHttpRequest, textStatus) {
}
});
Additionally you should use "name" instead "value" in #RequestParam:
#PostMapping(value = "/export")
#ResponseBody
public ResponseEntity<String> bulkExport(
#RequestParam(name = "managedObjects", required = false) List<String> managedObjects) {
//data
}
);
I think the problem is just with list that you want to send in your request.
var dataToSend = {
list: [{ fieldname: 'ABC' }, { fieldname: 'DEF' }]; // your list should something like this.
$.ajax({
type: "POST",
//url: "policy/js_policy",
url: "/export/?managedObjects=" + Mos ,
async: false,
data: JSON.stringify(dataToSend),
contentType: "application/json; charset=utf-8",
dataType: "json",
complete: function (XMLHttpRequest, textStatus) {
}
});
Related
I am sending a form data using AJAX and my Spring MVC controller is getting null value, but if I use Postman it will work fine.
Here is my ajax call:
$.ajax({
type: "post",
url:"http://localhost:8080/fivenet/mobile/api/login",
crossDomain: true,
data: JSON.stringify(formData),
dataType: 'json',
contentType: 'text/html;charset=UTF-8',
//contentType: 'application/json;charset=utf-8',
beforeSend: function(xhr){
},
success: function(msg){
console.log('sucess');
},
error: function(msg){}
Here is my controller:
#CrossOrigin(origins = "*", maxAge = 3600)
#RequestMapping(value = "create-account", method = RequestMethod.POST)
public #ResponseBody RespondMessage createAccount(
#RequestParam(value = "data", required = true) String dataString,
HttpServletRequest request) throws Exception {
RespondMessage responsed = new RespondMessage();
headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
JSONObject data = new JSONObject(dataString);
return responsed;
}
I have finally solved the problem. Its the way I serve my data.
$.ajax({
type: 'POST',
url: "http://localhost:8080/fivenet/mobile/api/login",
crossDomain: true,
data: {"data": JSON.stringify(formData)},*** This is where the error is
dataType: 'json',
beforeSend: function(xhr){
},
success: function(msg){
console.log('sucess');
},
error: function(msg){}
thanks all for your concerns
Try this code:
var formData = {"username" : "username", "password" : "password"};
$.ajax({
type: "POST",
url: "http://localhost:8080/fivenet/mobile/api/login",
data: JSON.stringify(formData),
dataType: 'json',
contentType: 'application/json;charset=utf-8',
beforeSend: function(xhr){
},
success: function(data){
alert(data)
},
failure: function(errMsg) {
alert(errMsg);
},
error: function(errMsg){
alert(errMsg);
}
I need to send data with id,name , and file (PdfBytes) byte[] with ajax to my service.
How can i add my PDF file to var pdf and add it to my ajax.
My code
var PdfBytes;
//Tried to fill PdfBytes with get,didnt work
$.get('http://testservices.xxx/PdfService/MYTest.pdf', function(data)
{
PdfBytes=data;
});
var ConvertHtmlToPdfAndSendEmail = {
"PdfBytes":PdfBytes,
id": id,
"Name": name
};
$.ajax({
type: "POST",
data: JSON.stringify(ConvertHtmlToPdfAndSendEmail),
dataType: 'json',
url: "http://testservices.xxx/ConvertHtmlToPdfAndDownload",
contentType: 'application/json; charset=utf-8',
async: true,
cache: false,
success: function (result) {
//my code
},
error: function (req, err) {
//my code
}
})
In the server i get PdfBytes is null
function expect to get byte[] PdfBytes
Sow how i can upload my pdf from my pc to var PdfBytes ,and send it in ajax to my service.
There two way to send byte[] in Ajax
You convert byte[] to string for GET or to json for POST
=> The main thing you should convert to byte array to text
and recover the data format when call the server script
$.ajax({
type: "GET",
url: "http://testservices.xxx/ConvertHtmlToPdfAndDownload?data="+encodeURI(byte_array.join())
});
$.ajax({
type: "POST",
dataType: "json",
data: JSON.stringify(byte_array),
url: "http://testservices.xxx/ConvertHtmlToPdfAndDownload"
});
Hope it help!
I think you should use option call 'async' so do that:
var PdfBytes = $.ajax({
url: 'http://testservices.xxx/PdfService/MYTest.pdf',
type: 'GET',
async: false
});
var ConvertHtmlToPdfAndSendEmail = {
PdfBytes: PdfBytes,
id: id,
Name: name
};
$.ajax({
type: "POST",
data: JSON.stringify(ConvertHtmlToPdfAndSendEmail),
dataType: 'json',
url: "http://testservices.xxx/ConvertHtmlToPdfAndDownload",
contentType: 'application/json; charset=utf-8',
async: true,
cache: false,
success: function (result) {
//my code
},
error: function (req, err) {
//my code
}
});
Hope it help.
I am using jQuery Ajax method with Asp.net MVC 3.0
My jQuery Code is
$.ajax({
type: "POST",
url: "/HomePage/GetAllCategories",
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (result) {
alert(result);
}
});
And my Action Method is
public JsonResult GetAllCategories()
{
return Json(null, JsonRequestBehavior.AllowGet);
}
I am getting the Error
POST http://localhost:50500/HomePage/GetAllCategories 405 (Method Not
Allowed)
My debugger is not hitting this method.
You have created GET method in the controller and you have set method type as POST in your jquery AJAX call.
$.ajax({
type: "GET",
url: "/HomePage/GetAllCategories",
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (result) {
alert(result);
}
});
Just add "/" on the end of the URL:
url: "/HomePage/GetAllCategories/",
Okay, try this. I am using a getJson call to try and fetch the same data.
$.getJSON("/HomePage/GetAllCategories",
function(returnData) {
alert(returnData);
});
Set type GET in the ajax call:
$.ajax({
type: "GET",
url: '#Url.Action("GetAllCategories","HomePage")' ,
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (result) {
alert(result);
}
});
and action:
[HttpGet]
public JsonResult GetAllCategories()
{
return Json(null, JsonRequestBehavior.AllowGet);
}
If want to do via POST then:
$.ajax({
type: "POST",
url: '#Url.Action("GetAllCategories","HomePage")' ,
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (result) {
alert(result);
}
});
and action:
[HttpPost]
public JsonResult GetAllCategories()
{
return Json(null, JsonRequestBehavior.AllowGet);
}
I have this code:
var SotriesModel = can.Model.extend({
findAll: 'GET /api/stories/',
create: function(story) {
console.log(story)
return $.ajax({
url: '/api/stories/',
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(story)
});
},
update : function(story) {
console.log(story)
return $.ajax({
url: '/api/stories/',
type: 'PUT',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(story)
});
},
destroy: 'DELETE /api/stories/{id}'
}, {});
var story = new SotriesModel({id:123123, name:"test"});
story.save();
What I expect from save is a PUT request with a JSON in payload, but what I get is just the id number:
123123
The update method signature for using a function is can.Model.update: function(id, serialized) -> can.Deffered.
So as the first parameter you always get the id and as the second the serialized data.
Changing your update : function(story) {} to update : function(id, story) {} should solve the problem.
<WebMethod()> Public Shared Function gtet() As String
...
Dim GET = uClass.GetSets(dbuser, dbparam1)
...
End Function
and
$(document).ready(function ()
{
data = { };
var jsondata = $.toJSON(data);
$.ajax({
type: "GET",
url: "index.aspx/gtet",
data: jsondata,
contentType: "application/json; charset=utf-8",
dataType: "text json",
beforeSend: function (xhr)
{
xhr.setRequestHeader("Content-type",
"application/json; charset=utf-8");
},
success: function (cget)
{
alert(cget);
},
error: function (XMLHttpRequest, textStatus, errorThrown)
{
window.location.reload();
}
});
}
Am I doing this right? I need to pull the string from Dim GET
Send the json as a parameter.
data: {
"json": jsondata
},
Also, make sure your webmethod is returning valid json.