Error 404 RESTFul Service with Ajax - ajax

I got a RESTful service call with ajax :
var request = $.ajax({type: "GET",
type : "GET",
url : "/services/equipment/searchEquipments?pId="+id,
cache : false
});
The java method in the service is declared as :
#GET
#Path("/searchEquipments/{pId}")
#Produces("application/json; charset=UTF-8")
public List<EquipmentVO> searchEquipments(#PathParam("pId") String pId){
I got a 404 return code.
I can't understand because after the call, the 404 code is on a weird URL :
http://localhost:7001/services/equipement/searchEquipments?pId=00192772&_=1408446932784
I can't figure why there's &_=1408446932784 at the end ??? It's not a part of the made URL during the call.
Any idea ?

RESTfull service does not accept argument in this way.
the argumant should be a part of the path (as it is defined)
so you should send a get request to "/services/equipment/searchEquipment/"+id
(without the ?pId=)
for example, if the Pid is 123, the path should be
/services/equipment/searchEquipment/123

Related

HttpMessageNotReadableException: Required request body is missing (Occasional) - SpringMVC Default Controller Request Mapping HTTP Method?

I have the following code which represents an Ajax POST request:
#RequestMapping("/participant/insertEvent")
public boolean insertEvent(Principal principal, #RequestBody String json, HttpServletRequest request) throws Exception {
//...
//return true or false
}
JS:
$.ajax({
type : "post",
dataType : "json",
url : '/app/participant/insertEvent',
data : JSON.stringify({'p1': p1, 'p2' : p2})
});
The app is deployed in Production and everything has been working with many users the whole week.
This morning I got the following production issue at this method (insertEvent):
org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public boolean
app.controller.participant.AjaxOperationsController.insertEvent(java.security.Principal,java.lang.String,javax.servlet.http.HttpServletRequest) throws java.lang.Exception at
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:161) at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:130) at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:124) at
org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161) at ...
I'm wondering, is it because I didn't explicitly specify this is a method={RequestMethod.POST} ?
But if I don't specify an HTTP method, what is the default?
However, if this is the issue, then why does the app work 99% of the time, and not for that one user?
FYI, the error was caused by including in the JSON an un-encoded % (percent) special char coming from a TextArea value.

Spring + Angular: How to parse ResponseEntity in angular?

I'm using Spring Boot to create an API that needs to be consumed in Angular 4. Spring and Angular are on different ports.
The problem is that Spring's ResponseEntity raises an error in Angular.
#RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity getFlow(#PathVariable int id) {
Flow flow = flowService.findById(id);
return new ResponseEntity(flow, HttpStatus.FOUND);
}
Now, I can perfectly use Postman to test the API and it works.
But when I make a request from Angular, it returns an error:
Strangely, it returns an error alongside the requested object.
Now, the cause of the problem is that the Spring Boot application returns a ResponseEntity and not a normal object (like String), and Angular doesn't know how to interpret it. If the controller returns just a Flow object, it works.
How can it be solved using ResponseEntity? Or, how else can I send the object alongside the HTTP status code?
Also, in #RequestMapping put produces = "application/json", and in get request in angular, add http options :
const httpOptions = {
headers: new HttpHeaders({
'Accept': 'application/json',
'Content-Type': 'application/json'
})
};
So your get request looks like this:
this.http.get(url, httpOptions)
As per the document mentioned here
https://docs.angularjs.org/api/ng/service/$http
A response status code between 200 and 299 is considered a success status and will result in the success callback being called. Any response status code outside of that range is considered an error status and will result in the error callback being called. Also, status codes less than -1 are normalized to zero. -1 usually means the request was aborted, e.g. using a config.timeout. Note that if the response is a redirect, XMLHttpRequest will transparently follow it, meaning that the outcome (success or error) will be determined by the final response status code.
As you are sending an instance of ResponseEntity(HttpStatus.Found) whose Http status code is 302 which doesnt fall under the success range thats why error callback is called.
Try returning the content like this
return new ResponseEntity(flow, HttpStatus.OK);

No route matches [GET] error when sending a post request with ajax

Sending a post request via ajax but getting the error above, "No route matches [GET]".
ajax looks like this
$.ajax({
type: 'POST',
url: '/stage_progress',
data: {'subject_id' : subject, 'course_id' : course, "chapter_id" : chapter, "stage_id" : stage, "user_id" : user, "session" : "0", "completed" : "true"},
success: function(){
console.log('got it');
}
});
routes.rb
post '/stage_progress', to: 'stage_progress#track_progress'
If I run rake routes it shows that a post route to this url exists but for some reason the ajax is creating a "get" request. Any help explaining why and how to fix it would be greatly appreciated.
When looking into your code, you are overriding the default index action of your controller 'stage_progress'. Calling '/stage_progress' will only route to the default index action(get). I guess you either rename the route '/stage_progess' to something else and that would solve the issue.
Or in other case you still want the same route, then try writing it above all other routes,
post '/stage_progress', to: 'stage_progress#track_progress'
resources 'stage_progress'
Because the routes are always mapped from top to bottom in the routes.rb file. If there is any other 'GET' route above the 'POST' route declaration, it will still take only the first route configured.

Ajax POST to WCF Rest CORS-compliant WebService throws error 405

I'm doing some test over WCF REST WebServices and i'm stuck with the POST call.
I've created a webservice that exposes some test data about the good ol' Northwind DB and since i wish to consume it locally from a test HTML page and since i'd like to test CORS capabilities, i made it CORS compliant by following these instruction http://enable-cors.org/server_wcf.html.
Unfortunately problems comes out when i make POST calls.
Unlike GET calls (works very well), POST call throws this error:
What the hell is it? it seems that "Access-Control-Allow-Origin" header is not correctly managed client-side, beacuse in my EnableCrossOriginResourceSharingBehavior WCF class, the method "ApplyDispatchBehavior" (it filter "Access-Control-Allow-Origin" headers of the arrival requests) is hit when i make a POST call, but then Ajax call fails.
This is my jQuery Ajax post command:
//Create new object
var item = {
"CustomerId": "0",
"CompanyName": "prova"
};
//Push object
$.ajax({
type: "POST",
url: 'http://localhost:3434/NorthwindService.svc/Customer/Create',
crossDomain: true,
headers: {'Access-Control-Allow-Origin' : '*'},
data: JSON.stringify(item),
success: function (data) {
alert('ok!');
},
contentType: 'application/json; charset=utf-8',
dataType: 'json'
});
This is my WCF service Visual Studio 2013 project.
To test it, you only have to set "NorthwindConnectionString" in web.config to an existing one. The webservice method that i've problem with, is the POST to the "http://localhost:3434/NorthwindService.svc/Customer/Create" method, all the others works fine.
This is a preview of my method contract:
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, UriTemplate = "Customer/Create", BodyStyle=WebMessageBodyStyle.WrappedRequest)]
void NewCustomer(CustomerDTO customer);
Thanks in advance.
I don't know what's going on, but thanks to supertopi and his link, i did the right steps to make it works. Unfortunately implementing all things discussed in here How to handle Ajax JQUERY POST request with WCF self-host did't works. I continued to get "405 Method not allowed" even by creating a new project.
The only thing that works in my case is the following:
1) Implement CustomHeaderMessageInspector and EnableCrossOriginResourceSharingBehavior classes and edit web.config as exposed in http://enable-cors.org/server_wcf.html.
2) Create in the service contract the following method:
[OperationContract]
[WebInvoke(Method = "OPTIONS", UriTemplate = "*")]
void GetOptions();
3) Implementing it empty.
public void GetOptions()
{
}
It sounds crazy, but it actually works.
If i remove GetOptions() operation contract, i continue to get 405 error on my client. If i implement it like indicated by supertopi's link (obviously after remove all stuff created in the step 1), it doesn't work either.
Hope it helps.
Your HTTP Request Method is defined OPTIONS instead of POST.
That is why you get HTTP Response 405 Method not Allowed (no handler for OPTIONS request)
Change the type parameter in jQuery ajax constructor to "POST" and the request is routed to correct handler.

ajax json request , always returning error

Hello i've got a problem with ajax json request. Im always getting an error, even if the requests are succeeded. At the moment i have this code:
function sumbitLoginForm(user, pass) {
if (user.trim() == '' || pass.trim() == '') {
alert("You must enter username and password!");
} else {
$.ajax({
type : 'POST',
url : 'https://url.php',
dataType : 'json',
data : {
userlogin : user,
userpass : pass
},
contentType: "application/json;",
success : function(data) {
$("#images").html("uspeshno");
},
error : function(data) {
$("#images").html("greshka");
}
});
}
return false;
}
$(document).ready(function() {
clearPageInputs();
$("#submitButton").click(function() {
sumbitLoginForm($("#username").val(), $("#password").val());
});
});
Im always getting an error , no matter what username and password i type . But the status of request is changing , if i type correct user and pass i get status 302 Moved temporarly , but when i type wrong user or pass i get status 200 OK . What am i doing wrong ?
PRG Pattern and Ajax
It looks like your server returns a HTTP 200 status code when the userid and password will not validate. This is proper behavior, as HTTP error codes not meant for application errors, but for HTTP protocol errors.
When the userid and password are matched succesfully, you are redirected to another page. This is also normal behavior, e.g. to prevent other people to re-use your login credentials using the back key.
This is called the Post/Redirect/Get pattern.
See: http://en.wikipedia.org/wiki/Post/Redirect/Get
The problem is that the PRG pattern does not play nice with Ajax applications. The redirect should be handled by the browser. It is therefore transparent for the jQuery code. The Ajax html response will be the page that is mentioned in the Location header of the 302. Your Ajax application will not be able to see that it is being redirected. So your are stuck.
In one of my projects I solved this on the server side. If I detected an Ajax call, I would not send a redirect but a normal 200 response. This only works if you have access to the server code.
If you cannot change the redirect for your Ajax calls, then you can parse the response headers or the html to see if you were being redirected and act accordingly. Probably the login will set a cookie, so you might try and look for the presence of that cookie.

Resources