2016-09-03 2 views
0

Ich versuche AJAX asynchron mit dem Server zu kommunizieren. Allerdings bin ich die folgende Fehlermeldung erhalten, Jede Idee ?:org.springframework.web.servlet.PageNotFound.handleHttpRequestMethodNotSupported Anforderungsmethode 'GET' nicht unterstützt

org.springframework.web.servlet.PageNotFound.handleHttpRequestMethodNotSupported Antrag Methode 'GET' nicht unterstützt

Controller:

package com.math.mvc; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.ResponseBody; 

@Controller 
public class PiCalculatorController { 
    @RequestMapping(value = "/picalculator", method = RequestMethod.GET) 
    public @ResponseBody String piCalculator(@RequestParam(value = "precision") int pr, @RequestParam(value = "decimals") int de) { 
     return String.valueOf(pr * de); 
    } 
} 

Javascript:

var getPiCalculation; 
$(document).ready(function() { 
    getPiCalculation = function() { 
     if ($('#precision').val() != '' || $('#decimals').val() != '') { 
      $.getJSON(
       "picalculator", 
       { 
        precision: $('#precision').val(), 
        decimals: $('#decimals').val() 
       }, 
       function (data) { 
        alert("response received: " + data); 
       } 
      ); 
     } else { 
      alert("Both fields need to be populated!!"); 
     } 
    }; 

Headers:

enter image description here

Antwort

0

Ich habe gerade auf den Header gesehen Screenshot, dass die Antwortinhalt-Typ text/html gesetzt. Dies muss auf application/json eingestellt werden.

Lösung:

Es ist erforderlich, auf der @RequestMapping hinzuzufügen:

produziert = "application/json"

So ist der Controller geändert werden muss wie folgt.

Controller:

package com.math.mvc; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.ResponseBody; 

@Controller 
public class PiCalculatorController { 
    @RequestMapping(value = "/picalculator", method = RequestMethod.GET, produces = "application/json") 
    public @ResponseBody String piCalculator(@RequestParam(value = "precision") int pr, @RequestParam(value = "decimals") int de) { 
     return String.valueOf(pr * de); 
    } 
} 

Danach Sie in den Header der folgenden erhalten:

enter image description here

Verwandte Themen