-1

I've read 3/4 posts on Stack plus many other examples to try figure this out but I've no clue ! Need some pointers please !!

Creating my first Ajax update through Spring-MVC and I keep getting a Status 415 being returned by my submission with The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request accept

JQuery... Version 3.1.1

function updateScore () {
        $("div#results").append("<p>Posting User/Game ID "  + this.id + " Value " + this.value + "</p>");

        var prediction = {}
        prediction["id"] = this.id;
        prediction["value"] = this.value;

        $.ajax({
            type : "POST",
            contentType : "application/json",
            url : "/tournament/setPrediction.html",
            data : JSON.stringify(prediction),
            dataType : 'json',
            timeout : 100000,
            success : function(data) {
                console.log("SUCCESS: ", data);
                displayResult(data, "success");
            },
            error : function(e) {
                console.log("ERROR: ", e);
                displayResult(e, "error");
            },
            done : function(e) {
                console.log("DONE");
                displayResult(true, "done");
            }
        });
    }

Controller... Spring version 4.3.5

@RestController
public class PredictionAjaxController {

@ResponseBody
@RequestMapping(value = "/setPrediction.html", consumes = MediaType.APPLICATION_JSON_VALUE,
    method = RequestMethod.POST, headers="Accept=application/json")
public Prediction setUserPrediction(@RequestBody PredictionPojo prediction) {
    Prediction result = new Prediction();

    System.out.println("AJAX call made in controller");

    return result;
}
}

Finally a very simple POJO for the JSon to map to

public class PredictionPojo {

private String id;
private String value;

Getters & Setters... ()
}

I've added different things onto the controller now to try and resolve, didn't start with it all ! I'm completely confuddled !

Should be so simple...

  • DH
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • it might be the URL, what happens when you open in your browser /setPrediction.html? what does the console says about the Ajax request? – Frederick Álvarez Mar 03 '17 at 14:26
  • HTTP Status 405 - Request method 'GET' not supported - get that when I access the URL directly... http://localhost:8080/tournament/setPrediction.html... Shows the controller and page is set up ? My assumption that I get the error back means its reaching the controller ? – Dangerous Hamster Mar 03 '17 at 14:28
  • `org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported WARNING: Request method 'POST' not supported` Posted to the Eclipse console, same error returned in console of Chrome Amended the POST to GET within JQuery as well and got the following within the eclipse console `INFO: Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level. java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986` – Dangerous Hamster Mar 03 '17 at 14:43
  • Please provide your MVC configuration. – Sotirios Delimanolis Mar 03 '17 at 15:46

1 Answers1

-2

You have an error in your ajax call, you are sending a string instead of a JSON object. Also I don't think is necessary to specify the consumes and headers attributes in you @RequestMapping annotation in your setUserPrediction method, The PredictionAjaxController is already defined as a RestController. Your ajax should be:

        $.ajax({
            // .......

            data : prediction,

            // .......
        });
artemisian
  • 2,976
  • 1
  • 22
  • 23
  • Whoever down voted this question should know a lot about HTTP requests! Can you please specify what is wrong with the answer so we all can learn? – artemisian Mar 03 '17 at 15:04
  • You need to serialize the javascript object before sending it. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify And here, just an example if you do not use it: http://stackoverflow.com/questions/12693947/jquery-ajax-how-to-send-json-instead-of-querystring – alfcope Mar 03 '17 at 15:25
  • @alfcope, although data can be a string it can also be an object as specified in the jquery documentation, so that's not a valid point. It doesn't provide a solution to the question but is not wrong. I usually use object instead of string and let jquery handle the rest. As an example please wonder how are you going to send a file to a server if you always stringify your object. – artemisian Mar 03 '17 at 15:38
  • If you do that you are not sending json objects. Please, read again the ajax documentation. If you send a plain object "It is converted to a query string, if not already a string. It's appended to the url for GET-requests". You want the user to change something that is not wrong and for that you are given to him something that will not work. So your answer is complete wrong. And your example is pointless. To send a file you need to use multipart/form-data as content-type. It is a different case. Are you going to receive an file on a controller waiting for a json? @artemisian – alfcope Mar 03 '17 at 16:40