【问题标题】:using ajax send large json data to controller使用 ajax 将大型 json 数据发送到控制器
【发布时间】:2013-08-13 12:30:02
【问题描述】:

这是我的JsonExample.jsp 文件,

    <!DOCTYPE html>
        <html>
        <title>jQuery Function Demo - jQuery4u.com</title>
        <head>
        <script
            src="http://www.jquery4u.com/function-demos/js/jquery-1.6.4.min.js"></script>
        <script src="http://www.jquery4u.com/scripts/function-demos-script.js"></script>


        <script>
            function call() {

                //var url = 'http://192.168.10.82:8081/formulator-service/tracks?callback_track=processJSON';

                var url = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=processJSON&tags=monkey&tagmode=any&format=json";

                $.ajax({
                    type : "GET",
                    url : url,
                    async : false,
                    jsonpCallback : "processJSON",
                    contentType : "application/json",
                    dataType : "jsonp",
                    success : function(json) {

                        alert(json.title);
                        //alert(json.name);

                        controllerCaller(json);

                    },
                    failure : function() {
                        alert("There is some error");
                    }
                });

            }
            function controllerCaller(json) {
                //var dummy = "poiuytre";

                alert("in the Controller caller");
                //var url="/controllerCall";
                $.ajax({
                    type : "POST",
                    url : "controllerCall",
                    async : false,
                    data : {
                        jsondata : json
                    },
                    success : function() {
                        alert("In controller Ajax function");
                    },
                    failure : function(error) {
                        alert("somthing went wrong");
                    }

                });
            }
        </script>


        </head>
        <body>
            <button onclick="call()">Get Json</button>
        </body>
        </html>

这是我的BaseController.java 文件,

package com.web.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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
@RequestMapping("/")
public class BaseController {

    @RequestMapping(method = RequestMethod.GET)
    public String printHello(ModelMap model) {

        return "Jsonpexample";

    }

    @RequestMapping(value = "/controllerCall", method = RequestMethod.POST, produces = "json/application")
    public @ResponseBody
    String controllerCall(
            @RequestParam(value = "jsondata", required = true) String data) {
        System.out.println("this is in the second function");

        return data;

    }

}

问题是控制器无法接收视图发送的数据。任何人都可以提出一个正确的方法吗?谢谢。

【问题讨论】:

  • 无法发送是什么意思?你有错误吗?
  • 您可以尝试将consumes="application/json; charset=utf-8" 添加到RequestMapping 吗?
  • 问题是我的json数据太长,无法通过GET方式发送,所以只好使用POST方式。但是如果我尝试执行这段代码,写在上面。它不起作用。

标签: java ajax spring spring-mvc


【解决方案1】:

您要发送到的 URL 是:“controllerCall” 这应该是一个 URL,就像您在函数 call 中使用的一样。

【讨论】:

  • 这是正确的,我想要的是通过使用 POST 方法将我收到的 json 数据发送到控制器,使用 GET 方法,如果我更改 ajax“类型:GET”,它的简单和上面的代码可以工作在controllerCaller 函数和“method = RequestMethod.POST”到“@RequestMapping(value = "/controllerCall", method = RequestMethod.POST,produces = "json/application")”中的“method = RequestMethod.GET”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多