账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    ajax传值给controller,controller提取传过来的值?
    42
    0

    问题描述

    我想在页面上写AJAX,将一个input中的值传给controller中的action,并且取出调用,请大家指点下

    问题出现的环境背景及自己尝试过哪些方法

    相关代码

    // 请把代码文本粘贴到下方(请勿用图片代替代码)
    $(function () {

            $.ajax({
                type: 'post',
                url: '/CustomsInspections/GetData',
                dataType: "json",
                //contentType: "application/json",
                data: {
                    operation_id: $("#Operation_ID").val(),
                    fight_no: $("#Flight_NO").val(),
                    mbl: $("#MBL").val(),
                    consign_code_ck: $("#Consign_Code_CK").val(),
                },
                success: function (data) {
                    if (data == "success") {
                        console.log("成功");
                    }
                    else {
                        console.log("失败");
                    }
                }
            });
        })
    
    

    public ActionResult GetData(string operation_id,int page = 1, int rows = 10, string sort = "Id", string order = "asc", string filterRules = "")

        {
            var filters = JsonConvert.DeserializeObject<IEnumerable<filterRule>>(filterRules)??new List<filterRule>();
            var ArrFilter = new List<filterRule>();
    
            string  fight_no = Request["fight_no"].ToString();
            string  mbl = Request.Form["mbl"].ToString();
            string  consign_code_ck = Request.Params["consign_code_ck"].ToString();
    

    你期待的结果是什么?实际看到的错误信息又是什么?

    我看网上这样写,可是取到的fight_no,mbl,consign_code_ck都是null

    1
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 執筆訴情殤 普通会员 1楼
      在使用Ajax向Spring MVC或其他类似的后端框架的Controller发送请求并传递参数时,您可以按照以下步骤进行操作: 1. 首先,前端(JavaScript/jQuery)发起Ajax请求,并将数据作为参数传递: ```javascript $.ajax({ type: "POST", // 或者 GET、PUT、DELETE等 url: "/your-controller-endpoint", // Controller中的处理方法路径 data: JSON.stringify({key1: 'value1', key2: 'value2'}), // 要传递的数据,这里以JSON格式为例 contentType: "application/json; charset=utf-8", dataType: "json", success: function(response) { // 处理返回的成功响应 }, error: function(xhr, status, error) { // 处理错误 } }); ``` 2. 然后,在后端的Controller中,通过`@RequestBody`注解来接收并解析传过来的JSON数据: ```java import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class YourController { @PostMapping("/your-controller-endpoint") public ResponseEntity handleAjaxRequest(@RequestBody YourRequestObject request) { String value1 = request.getKey1(); String value2 = request.getKey2(); // 在这里对value1和value2进行处理... return ResponseEntity.ok("Success"); } // 定义一个类来映射前端传来的JSON对象 public static class YourRequestObject { private String key1; private String key2; // getter和setter方法... } } ``` 在这个例子中,前端通过Ajax发送了一个JSON对象,后端通过`@RequestBody`注解将其自动绑定到`YourRequestObject`这个Java对象上,然后您就可以直接通过对象的getter方法提取出这些值了。
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部