Friday, November 14, 2014

Problems with Forefront Unified Access Gateway (UAG) and the PUT verb




Problem:
We were seeing this error when updating data using the PUT verb
net::ERR_CONNECTION_RESET message
SyntaxError: Unexpected end of input {stack: (...), message: "Unexpected end of input"}

Solution:
Ultimately, I believe the problem was a result of letting WEB API return a void to the calling client.  This results in a 204 "No Content" being returned to the user.  It worked fine internally, but externally via UAG the data was being saved and client was seeing an error.

Now, we are returning an OK like this:
[HttpPut]
public HttpResponseMessage Update(UserViewModel value)
{
        // Update stuff
return Request.CreateResponse(HttpStatusCode.OK);
}

Also, make sure that your Ajax call does NOT include a dataType.
function update(someJavaScriptObject) {
return $.ajax({
url: '/api/Users',
type: 'PUT',
data: someJavaScriptObject,
      dataType: "json"
 });
};

Another possible problem:
Returning primitives from WEB API controller methods and specifying a dataType: "json" in Ajax call.

Another possible solution:

  • Assuming you have the json formatter setup in the WebApiConfig:
    • Just wrap it in a class so that it is turned into a JSON object,  
    • You could also use a anonymous class and CreateResponse method as in:
      return Request.CreateResponse(HttpStatusCode.OK,  new {id = 344});  
  • Remove dataType from the ajax call so that your call accepts *.* and then return the primitive from the WEB API method like this:
    return Request.CreateResponse(HttpStatusCode.OK, 344);  



No comments: