here is my code
$.ajax({
url: "DataGridServlet.htm",
type: "GET",
traditional: true,
dataType: 'JSON',
cache: false,
contentType:"application/json",
success: function (response){
console.log(response);
}
});
});
and when i am sending request to controller.. every is working but wnem returning JSONObject i am getting thya status code as 406 not acceptable.
and below spring controller code
#RequestMapping(value="/DataGridServlet.htm", method = RequestMethod.GET,produces="application/json",headers="Accept=*/*")
public #ResponseBody JSONObject getReturnData()
{
System.out.println("control came into conroller");
JSONObject dataObject=new JSONObject();
dataObject=jqTabsGridDataDao.getTabsData();
System.out.println("controller data"+dataObject);
return dataObject;
}
any one can help me?
Change
#RequestMapping(value="/DataGridServlet.htm", method = RequestMethod.GET,produces="application/json",headers="Accept=*/*")
to
#RequestMapping(value="/DataGridServlet.htm", method = RequestMethod.GET,produces="application/json",)
and this
$.ajax({
url: "DataGridServlet.htm",
type: "GET",
traditional: true,
dataType: 'JSON',
cache: false,
contentType:"application/json",
success: function (response){
console.log(response);
}
});
});
to
$.ajax({
url: 'DataGridServlet.htm',
type: "GET",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
success: function(result) {
alert(result + " result ");
},
error: function(resError) {
//alert(resError + " Error ");
}
});
Related
product code on /produkidbypulsa/ does not exist, should be /produkidbypulsa/1/
How do I fix it?
this is function getCode
function getCode() {
var kode_produk = $('#pulsa').val();
// pulsa.innerHTML = "";
$.ajax({
type: 'GET',
url: '/produkbyidpulsa' + '/' + kode_produk,
dataType: 'JSON',
success: function (data) {
// pulsa.innerHTML +=
$('#kodeproduk').val(data.kode_produk)
$('#pulsa_code').val(data.pulsa_code)
$('#totalweb').val(data.harga_beli)
var kodeprod = data.kode_produk
$('#total').val(data.harga_jual)
$('#harga').html(data.harga_jual)
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
})
$.ajax({
type: 'GET',
url: 'https://testprepaid.mobilepulsa.net/v1/legacy/index/pulsa' + kodeprod,
dataType: 'JSON',
processData: false,
contentType: 'application/json',
CrossDomain: true,
async: false,
success: function (data) {
$('#totalweb').val(data.harga_beli)
}
})
}
})
// });
}
and this is onclick for call getCode()
<div class="col-md-12" id="pulsa" name="pulsa" onclick="getCode()"><label for="harga"></label></div>
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) {
}
});
function regCall(token){
$.ajax({
type: 'GET',
url: 'http://localhost:3000',
dataType: 'HTML',
headers: {
'x-auth': token
}
});
}
This is my ajax GET request I want to display the given url in html.
The below is the whole snippet with my login logic.
$(document).ready(()=>{
$('#login').submit((e)=>{
$.ajax({
type: 'POST',
url:'http://localhost:3000/login/users',
data: {
email: $('#email').val(),
password: $('#password').val()
},
success: function(data, status, req){
// alert(req.getResponseHeader('x-auth'));
localStorage.setItem('t',req.getResponseHeader('x-auth'));
var token = localStorage.getItem('t');
regCall(token);
// window.location.href = '/';
},
error: function (req, status, error) {
// alert(req.getResponseHeader('x-auth'));
localStorage.setItem('t',req.getResponseHeader('x-auth'));
alert('Invalid email and password');
window.location.href = '/login';
}
});
e.preventDefault();
});
})
This is the whole code of snippet.
extract response data from SUCCESS function:
function regCall(token){
$.ajax({
type: 'GET',
url: 'http://localhost:3000',
dataType: 'HTML',
headers: {
'x-auth': token
},
success: function(data){
//targetElement should be replaced by the ID of target element
$("#targetElement").html(data);
}
});
}
By using the success callback function you can display the response content on the HTML place
**First method:**
function regCall(token){
$.ajax({
type: 'GET',
url: 'http://localhost:3000',
dataType: 'HTML',
headers: {
'x-auth': token
},
success: function(responseData){
$("#div or class Id").html(responseData);
}
});
}
**Second method:**
function regCall(token){
$.ajax({
type: 'GET',
url: 'http://localhost:3000',
dataType: 'HTML',
headers: {
'x-auth': token
}
}).done(function(responseData){
$("#div or class Id").html(responseData);
});
}
**NOTE:**
Make sure you are having the jQuery script
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
I'm currently working the project using Polymer and I'd like to get the return value of API after POST using Iron-Ajax.
Here is my sample code,
var rs = $.ajax({
type: "POST",
url: apiUrl,
data: _data,
dataType: "json",
contentType: 'application/json'
});
rs.done(function (data) {
console.log(data);
alert(data);
}
});
Ajax is asynchronous by default,you need add async:false to make it synchronous
var rs = $.ajax({
type: "POST",
url: apiUrl,
data: _data,
async:false,
dataType: "json",
contentType: 'application/json'
});
var result = null;
rs.done(function (data) {
console.log(data);
alert(data);
result = data;
}
});
//return result;//you can return value like this
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);
}