2016-09-03 4 views
0

Я пытаюсь использовать AJAX для асинхронного взаимодействия с сервером. Тем не менее, я получаю следующее сообщение об ошибке, Любая идея ?:org.springframework.web.servlet.PageNotFound.handleHttpRequestMethodNotSupported Метод запроса 'GET' не поддерживается

org.springframework.web.servlet.PageNotFound.handleHttpRequestMethodNotSupported метод запроса 'GET' не поддерживается

Контроллер:

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!!"); 
     } 
    }; 

Заголовки:

enter image description here

ответ

0

Я только что видел на коллекторах скриншот, что ответ типа содержимого устанавливается в текст/html. Это необходимо установить для приложения/json.

Решение:

Требуется добавить на @RequestMapping:

производит = "приложения/JSON"

Таким образом, контроллер должен быть изменен следующим образом.

Контроллер:

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); 
    } 
} 

После этого вы получите в заголовках следующие:

enter image description here