简体   繁体   English

未捕获的TypeError:无法读取属性toLowerCase的未定义

[英]Uncaught TypeError: Cannot read property toLowerCase of undefined

How can I include the token within all your Ajax requests using jQuery and spring security? 如何使用jQuery和Spring Security将令牌包含在所有Ajax请求中?

At the moment I get the following error 目前,我收到以下错误

Uncaught TypeError: Cannot read property 'toLowerCase' of undefined 未捕获的TypeError:无法读取未定义的属性“ toLowerCase”

Javascript: 使用Javascript:

function editUser(data) {

var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
var ctx = location.href.substring(0, location.href.lastIndexOf("/"));
var dataObject = JSON.stringify({ 'username': data });

$.ajax({
      url: ctx+'/users/edit',
      type: 'POST',
      beforeSend: function(xhr){
          xhr.setRequestHeader(header, token);
      },
      data: data
    });
}   

Controller: 控制器:

@RequestMapping(value = "/users/edit", method = RequestMethod.POST)
public String editUser(@RequestParam("username") String username) {

    System.out.println(username);
    return "users/edit";
}

toLowerCase function is in jQuery toLowerCase函数在jQuery中

// Caches the header
                setRequestHeader: function( name, value ) {
                    var lname = name.toLowerCase();
                    if ( !state ) {
                        name = requestHeadersNames[ lname ] = requestHeadersNames[ lname ] || name;
                        requestHeaders[ name ] = value;
                    }
                    return this;
                },

Either make sure that the page always has <meta name="_csrf" content="something"> and <meta name="_csrf_header" content="something"> tags, or have your AJAX code check that the values exist before trying to set the header: 请确保该页面始终具有<meta name="_csrf" content="something"><meta name="_csrf_header" content="something">标记,或者让您的AJAX代码在尝试尝试之前先检查值是否存在设置标题:

beforeSend: function(xhr) {
    if (header && token) {
        xhr.setRequestHeader(header, token);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的TypeError:无法读取未定义的属性“ toLowerCase” - Uncaught TypeError: Cannot read property 'toLowerCase' of undefined 未捕获的TypeError无法读取未定义的属性“ toLowerCase” - Uncaught TypeError Cannot read property 'toLowerCase' of undefined 未捕获的TypeError:无法读取未定义的属性'toLowerCase' - Uncaught TypeError: Cannot read property 'toLowerCase' of undefined 我有错误未捕获的TypeError:无法读取未定义的属性&#39;toLowerCase&#39; - I have the error Uncaught TypeError: Cannot read property 'toLowerCase' of undefined 未捕获的TypeError:无法在文本字段上读取未定义错误的属性“ toLowerCase” - Uncaught TypeError: Cannot read property 'toLowerCase' of undefined Error on text field 未捕获的类型错误:无法读取未定义的属性“toLowerCase”(待办事项列表应用程序) - Uncaught TypeError: Cannot read property 'toLowerCase' of undefined (Todo list application ) Elastic_Grid | “未捕获的TypeError:无法读取未定义的属性&#39;toLowerCase&#39;” - Elastic_Grid | “Uncaught TypeError: Cannot read property 'toLowerCase' of undefined” 获取错误未捕获的TypeError:无法读取未定义的属性“ toLowerCase” - Getting error Uncaught TypeError: Cannot read property 'toLowerCase' of undefined jsonp请求上的“ Uncaught TypeError:无法读取未定义的属性&#39;toLowerCase&#39;” - “Uncaught TypeError: Cannot read property 'toLowerCase' of undefined” on jsonp request TokenInput +未捕获的TypeError:无法读取未定义的属性“ toLowerCase” - TokenInput + Uncaught TypeError: Cannot read property 'toLowerCase' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM