Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

The problem is that field "roles" is a select list with multiple-choice, so the field has to be converted into an array. I could not find any piece of info about how to do it. Need help. I'm doing this:

$.ajax({
            url: '/api/users',
            async: true,
            dataType: 'json',
            contentType: "application/json",
            type: "PUT",
            data: {
                id : jQuery('#id').val(),
                username: jQuery('#username').val(),
                password: jQuery('#password').val(),
                roles :
            },
        })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
421 views
Welcome To Ask or Share your Answers For Others

1 Answer

As long as you are using a standard multiple <select> tag, the jQuery val() will already be an array of strings. You can specify it exactly the same as your username and password values.

<select id="roles" multiple>
  <option>Admin</option>
  <option>Editor</option>
  <option>User</option>
</select>

data: {
    id : jQuery('#id').val(),
    username: jQuery('#username').val(),
    password: jQuery('#password').val(),
    roles : jQuery('#roles').val()
},

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...