Getting SyntaxError: missing ; before statement during ajax JQuery Call - javascript

Code is:
var rootData = null;
$.ajax({
url: 'http://localhost:12345/request',
data: rootData,
type: 'GET',
crossDomain: true,
dataType: 'jsonp',
error: function() { alert('Failed!'); },
success: function() { alert('OK'); }
});
I've got :
"SyntaxError: missing ; before statement"
I don't understand why !

This error might occur when you're not escaping a string properly and the JavaScript engine is expecting the end of your string already.
Check rootData, it might be causing this error. (I'm assuming rootData is set to null then populated with some data before sending the request)

OK I found the solution:
a Rest Server is on port 12345 (Tomcat)
and a Spring MVC server on port 8080 (Tomcat)
the request from MVC is
var treeData = null;
$.ajax({
url: 'http://localhost:12345/request',
data: treeData,
type: 'GET',
async:false,
crossDomain: true,
dataType: 'json',
error: function() { alert('Failed!'); },
success: function( treeData ) { ....
and the rest server must accept the cross domain request just by adding #CrossOrigin to it :
#CrossOrigin(origins = "http://localhost:8080")
#RequestMapping(value = "/request", produces = { MediaType.APPLICATION_JSON_VALUE}, method = RequestMethod.GET)
public String request() { ....

Related

How to fix ajax issue with not displaying in console?

I have a simple ajax call in my Java spring boot application that calls a method in the controller and then returns the value to the front-end console. However, when I run the code it runs with a status of 400 but it does not show anything in my console. Not sure if I am forgetting anything or I have it setup wrong, but nothing is being passed back I am assuming.
JQuery:
$(".modalPathContentBtn").on("click", function(event) {
getSecondQuery();
});
function getSecondQuery() {
var search2 = {
"dtoTiername" : "Test"
}
$.ajax({
type : "POST",
contentType : 'application/json; charset=utf-8',
dataType : 'json',
url : "/ajax/mqlGetSecondQuery",
data : JSON.stringify(search2),
success : function(result) {
console.log("It works: " + result);
}
});
}
Java:
#RequestMapping(value = "/ajax/mqlGetSecondQuery", method = RequestMethod.POST)
public #ResponseBody String sendSecondQuery(#RequestBody TestApp mTestApp, HttpServletRequest request) {
String pathVal = mTestApp.getDtoTiername();
System.out.println("Test of if it is getting to this part of the code");
return "randomString";
}
You mentioned your request is failing with a status code of 400 which would mean the success of your ajax request would not get called since the request has not succeeded. You need to add a fail handler. It would look something like this.
$.ajax({
type: "POST",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
url: "/ajax/mqlGetSecondQuery",
data: JSON.stringify(search2),
success: function(result) {
console.log("It works: " + result);
},
fail: function(err) {
console.log(err)
}
});
This may not be exact syntax, but the idea here is that you have a fail handler

bad request 400 error while sending request to RestControler Spring MVC

I am getting bad request 400 error while sending request with parameters to controler I have checked whole sysntax but I did not wind any mistake, please look at my code whats wrong there?
var url = contextPath+"/billingControler/getOrdersByResWiseTables";
$.ajax({
url : url,
data : "&resID="+$("#rsId").text()+"&tblid="+tableId,
type : "get",
dataType : "json" ,
contentType : 'application/json; charset=utf-8',
success : function(response) {
console.log(response);
}
});
error :
jquery-3.3.1.min.js?_=1520931033076:2 GET http://localhost:8088/smartpos/billingControler/getOrdersByResWiseTables?&resID=11&tblid=3 400 (Bad Request)
please check my java code
#RequestMapping(value="/getOrdersByResWiseTables", method=RequestMethod.GET, produces="application/json")
public List<OrderBans> getOrdersByResWiseTables(#RequestParam("resId") String resId,#RequestParam("tblid") String tableid) {
String result="";
logger.debug("Started adding order");
RestypeIDao pdo = new RestypeIDaoImp();
List<OrderBans> orderList = pdo.getOrdersResWIseTbles(resId, tableid);
System.out.println(orderList);
logger.debug("end adding order");
return orderList;
}
You have extra & in your URL.
Also, you're using data attribute wrong, it should be an object.
Try this:
var url = contextPath+"/billingControler/getOrdersByResWiseTables";
$.ajax({
url : url,
data: {
"resID": $("#rsId").text(),
"tblid": tableId
},
dataType:"json",
contentType:'application/json; charset=utf-8',
success:function(response) {
console.log(response);
}
});

JQuery trouble with sending ajax report

I am trying to send report with this script:
$(".saveButton").on("click", function(){
var setting = {
'propertyName' : "SomeName",
'propertyValue' : $(this).parent().siblings('.inputCol').children('.inputField').val();
};
$.ajax({
type: "Post",
contentType: "application/json",
url: "/settings/PropertyEdit",
data: JSON.stringify(setting ),
dataType: 'json',
success: function (data) {
//Do something
}
});
And receive my POST in this method:
#Link(label = "Property Edit", family = "SettingsController", parent = "Settings")
#RequestMapping(value = "/settings/PropertyEdit", method = RequestMethod.POST)
public String atmPropertyEdit(Setting setting) {
return "true";
}
However I can even stop into my breakpoint (on return) (Error 403 - no-referrer-when-downgrade), unless I change my request method to GET, then it works fine.
Have tried this script - same 403 error.
$.post('/atmsettings/atmPropertyEdit', { propertyName : "hello", propertyValue : "hello2"},
function(returnedData){
console.log(returnedData);
});

"SyntaxError: expected expression, got '<'" when I used jsonp to cross domain in laravel

I used laravel5.2 in my project;
javascript:
$.ajax({
type: 'get',
url: 'http://XX.XX.XX.XX/account/status?callback=?',
dataType : 'jsonp',
contentType: "application/json",
jsonp : "callback",
jsonpCallback: "jsonpcallback",
success: function(data){
console.log(data);
},
error: function(){
alert('500 error!')
}
});
route:
Route::get('/account/status', 'HomeController#accountStatus');
controller:
public function accountStatus(Request $request)
{
return Response::json(array('name' => 'Steve', 'state' => 'CA'))->setCallback($request->input('callback'));
}
but the response information has the whole html file, it turns out my route in route.php doesn't work,because whatever I changed the request url, it returns the same error and shows:
SyntaxError: expected expression, got '<'

IE8: Access denied

I am trying to access and API using jquery/post but its not working in IE8. Its throwing Access denied error in IE8 only.
js code:
var url = 'http://somecomp.cartodb.com:80/api/v1/map?map_key=xxxxxxxxxxxxxxxxxxxx&stat_tag=API';
var data = //some long data of length greater than 3000
$.ajax({
crossOrigin: !0,
type: "POST",
method: "POST",
dataType: "json",
contentType: "application/json",
url: url,
data: JSON.stringify(data),
success: function(a) {
console.log('success');
},
error: function(a) {
console.log('error');
}
})
If I add ?callback=? at the end of url, it still fires the error callback but with statusText: 'success' and code: 200
here is full code: http://textuploader.com/ato0w
Change dataType to jsonp will allow you to make cross-domiain requests. This will work only with GET requests.
If you're using CORS for accessing cross-origin resource, try to add the following line:
$.ajax({
crossDomain: true, // replace "crossOrigin: !0;"
});
If this not working for you, try to add the following line above $.ajax() call.
jQuery.support.cors = true;

Categories