鍍金池/ 問答/Java  網(wǎng)絡(luò)安全  HTML/ 為何使用fetch始終不能把整數(shù)傳入后臺(tái)

為何使用fetch始終不能把整數(shù)傳入后臺(tái)

前端代碼

const api = "http://localhost:8080/index.do"
const headers = {
  'Content-Type': 'text/plain',
  'Access-Control-Allow-Origin':'*'
}
const num=0
getCourses = (num) =>
  fetch(`${api}`, {
    method: 'POST',
    headers: {
      ...headers,
      'Content-Type': 'application/json'
    },
    mode: 'cors',
    body: JSON.stringify({ num })
  }).then(res => res.json())
    .then(data => data)
getCourses(num)

后端代碼

package com.Xinweixunwang.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
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;

import com.Xinweixunwang.web.dao.CoursesDao;
import com.Xinweixunwang.web.model.Course;
import com.Xinweixunwang.web.service.CoursesService;

@CrossOrigin(origins = "*")
@Controller
public class Coursescontroller {
    @Autowired
    CoursesService coursesservice;
    
    @RequestMapping(value = "/index.do",method = RequestMethod.POST)
    @ResponseBody
    public List<Course> index(@RequestParam("num") String num){
        System.out.println(num);
        return coursesservice.getCourse(0,10);
    }
}

為何后端總是接受不到請(qǐng)求的參數(shù),Required String parameter 'num' is not present,各位大神幫忙解決一下啊

回答
編輯回答
蟲児飛

@RequestParam("num")改為@RequestBody試試

2017年2月2日 16:00
編輯回答
憶當(dāng)年

你確定你這個(gè)傳過來了,跨域都沒做處理吧,你這是post請(qǐng)求,屬于預(yù)檢請(qǐng)求,要先驗(yàn)證。

2017年8月1日 17:21