2016-09-03 11 views
0

サーバと非同期で通信するためにAJAXを使用しようとしています。しかし、私は次のエラーメッセージを取得しています、任意のアイデア?:org.springframework.web.servlet.PageNotFound.handleHttpRequestMethodNotSupportedリクエストメソッド 'GET'はサポートされていません

はサポートされていない 'GET' リクエストメソッドをorg.springframework.web.servlet.PageNotFound.handleHttpRequestMethodNotSupported

コントローラー:

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

私はちょうどヘッダーに見てきましたが、応答コンテンツ・タイプがtext/htmlのに設定されていることをスクリーンショット。これはapplication/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

関連する問題