jquery函数(post / get,输入表单中的数据)不起作用

时间:2015-05-05 07:54:40

标签: javascript jquery html

发布一些数据。从html表单获取用户名和密码。 我们在post请求中修复了jsons的格式。现在我收到了404找不到发布请求。任何想法?

我正在使用localhost,因为我使用的是sinatra应用程序。

我已经加入了html,javascript和main.rb

HTML:

<div class="form-bg" id="myForm">
    <form>
        <h2>Login</h2>
        <p><input type="text" id="username" placeholder="Username"></p>
        <p><input type="password" id="password" placeholder="Password"/><p>
        <label for="remember">
            <input type="checkbox" id="remember" value="remember" />
            <span>Remember me on this computer</span>
        </label>
        <button type="submit" id="submit"></button>
    <form>
</div>

使用Javascript:

$(document).ready(function() {

    /**
    * Post initial admin and a few users & groups
    */

    $.post("http://localhost:4567/main.rb", [
            {
            "name": "alex",
            "email": "alex@example.com",
            "password": "secret",
            "admin": true
            }, {
            "name": "alex",
            "email": "alex@example.com",
            "password": "password",
            "admin": false
            }, {
            "name": "bill",
            "email": "bill@example.com",
            "password": "password",
            "admin": false
            }
    ], function () {
        console.log("success");

});

$.post/("http://localhost:4567/main.rb", [
       {
            "name": "support",
            "members": ["admin"],
            "private": true
            }, {
            "name": "dev",
            "members": ["alex", "bill"],
            "private": false
            }
    ], function () {
        console.log("success");

});

    /**
    * Get username and password from the form
    */

    $("#myForm").submit(function() { // loginForm is submitted
        var username = $('#username').val(); // get username
        var password = $('#password').val(); // get password
        function showGetResult( username ) {
            var result = null;
            var scriptUrl = "http://localhost:4567/main.rb";
            $.ajax({
                url: scriptUrl,
                type: 'get/users.username',
                dataType: 'json',
                async: false,
                success: function(data) {
                    result = data;
                }
            });
            return result;
        }

        var user = showGetResult(username);
        function passwordCheck(user, password) {
            if (user.password === password){
                window.open = "http://localhost:4567/userprofile/userprofile.html";
            }
            else {
                document.getElementById("notice").style.display = "block";
                user = {};
            }
        }
        passwordCheck();
    })


    /**
    * Update DOM elements in userprofile.html with user information
    * DOM (groups, private groups, create new user, settings)
    * Groups is display only. Do a get, append data to dom
    * Private groups (ADMIN ONLY). Do a get, append data to dom
    * Create new user (ADMIN ONLY). Post request with info. (show notice "New user created")
    * Settings. Put request to update user info
    * (private groups editing feature - ADMIN ONLY)
    * (delete user option - ADMIN ONLY)
    */

})

main.rb的:

    get '/users' do
  {users: USERS.keys}.to_json
end

get '/users/:name' do
  halt 404 unless user = USERS[params[:name]]
  user.to_json
end

post '/users' do
  user = JSON.parse(request.body.read)
  halt 400 unless %w{name email password admin}.all?{|s| user[s]}
  halt 409 if USERS[user['name']]
  USERS[user['name']] = user
  201
end

put '/users/:name' do
  halt 404 unless USERS[params[:name]]
  user = JSON.parse(request.body.read)
  halt 400 unless %w{name email password admin}.all?{|s| user[s]}
  halt 400 unless user['name'] == params[:name]
  USERS[params[:name]] = user
  200
end

1 个答案:

答案 0 :(得分:1)

它应该是这样的:

$.post("http://localhost:4567/main.rb", [
    {
        "name": "admin",
            "email": "admin@example.com",
            "password": "secret",
            "admin": true
    }, {
        "name": "alex",
            "email": "alex@example.com",
            "password": "password",
            "admin": false
    }, {
        "name": "bill",
            "email": "bill@example.com",
            "password": "password",
            "admin": false
    }
], function () {
    console.log("success");

});
  1. 您需要将多个不同的对象放在一个匿名数组中,否则您需要添加额外的参数。
  2. 第一个参数是您的网址,第二个是您的数据,第三个是您的成功回调函数。
  3. 以下是一般语法:

    $.post('url_here', { /* all your data in here */ }, function(){ 
      /* success here */ 
    }).fail(function(){ 
      /* fail here */ 
    });
    

    <强>更新

    您的ruby启动404,因为您以错误的方式访问数据。我不知道如何在红宝石中做到这一点,因为我不知道红宝石,所以我会给你相同的js。

    要访问您发送的数据,您需要了解您必须通​​过所有信息循环(至少在js中)。

    例如,假设您想要访问一些不同的名称,而您的所有信息都位于名为data

    的变量中
    console.log(data[0].name); // this would output "admin"
    console.log(data[1].name); // this would output "alex"
    console.log(data[2].name); //this would output "bill"
    

    以下是你如何循环使用它。

    for(var i = 0; i < data.length; i++){
      console.log(data[i].name);
    }
    

    请告诉我这是否有用。