Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Backbone.js的POST方法发送数据_Javascript_Jquery_Backbone.js_Codeigniter 3 - Fatal编程技术网

Javascript 使用Backbone.js的POST方法发送数据

Javascript 使用Backbone.js的POST方法发送数据,javascript,jquery,backbone.js,codeigniter-3,Javascript,Jquery,Backbone.js,Codeigniter 3,我使用Backbone.js实现了一个简单的登录系统 我试图使用HTTPPOST方法将用户名和密码传递给处理用户身份验证的控制器类 public function sessions() { if ($this->input->server('REQUEST_METHOD') == 'POST') { $this->login(); } elseif ($this->input->server('REQUEST_METHOD') ==

我使用Backbone.js实现了一个简单的登录系统

我试图使用HTTPPOST方法将用户名和密码传递给处理用户身份验证的控制器类

public function sessions() {
    if ($this->input->server('REQUEST_METHOD') == 'POST') {
        $this->login();
    } elseif ($this->input->server('REQUEST_METHOD') == 'GET') {
        $this->index();
    } elseif ($this->input->server('REQUEST_METHOD') == 'DELETE') {
        $this->session->sess_destroy();
        $this->index();
    }
}
Backbone.js代码段:

$(document).ready(function() {

    var Session = Backbone.Model.extend({
        url: function() {
            var link = "http://<?php echo gethostname(); ?>/reddit/index.php/welcome/sessions";
            return link;
        },
        defaults: {
            username: null,
            password: null
        }
    });

    var model = new Session();

    var DisplayView = Backbone.View.extend({
        el: ".complete",
        model: model,
        type: 'POST',
        initialize: function() {
            this.listenTo(this.model, "sync change", this.gotdata);
        },
        events: {
            "click #signin": "getdata"
        },
        getdata: function(event) {
            event.preventDefault();
            var username = $("input#username").val();
            var password = $("input#password").val();
            this.model.set({ username: username, password: password });
            this.model.fetch();
        },
        gotdata: function() {
            console.log(this.model.get('username'));
            console.log(this.model.get('password'));
            $("#base-nav").load(location.href + " #base-nav");
            $("#signin-form").load(location.href + " #signin-form");
        }
    });

    var displayView = new DisplayView();
});
$(文档).ready(函数(){
var Session=Backbone.Model.extend({
url:function(){
变量链接=”http:///reddit/index.php/welcome/sessions";
返回链接;
},
默认值:{
用户名:null,
密码:null
}
});
var model=新会话();
var DisplayView=Backbone.View.extend({
el:“.完成”,
模型:模型,
键入:“POST”,
初始化:函数(){
this.listenTo(this.model,“sync change”,this.gotdata);
},
活动:{
单击“登录”:“获取数据”
},
getdata:函数(事件){
event.preventDefault();
var username=$(“输入#用户名”).val();
var password=$(“输入密码”).val();
this.model.set({username:username,password:password});
this.model.fetch();
},
gotdata:function(){
log(this.model.get('username');
log(this.model.get('password');
$(“#基本导航”).load(location.href+“#基本导航”);
$(“#登录表单”).load(location.href+“#登录表单”);
}
});
var displayView=newdisplayview();
});
我目前使用了
type
属性来定义HTTP方法类型POST。但这似乎不起作用,因为使用开发人员控制台只能观察GET请求

必须注意的是,当我删除
event.preventDefault()时防止在单击链接时重新加载页面()POST请求似乎已成功传递到后端,尽管页面重新加载阻止了预期的目标行为


如何使用Backbone.js的POST请求轻松发送数据?

您使用的是
this.model.fetch()用于检索数据。默认情况下,它发出GET请求,不发送正文或查询字符串中的任何数据

尝试查找选项和函数时,请使用。这本书也很短,很容易理解

快速修复 使用保存

this.model.save();
若要强制执行POST请求,例如当您已经登录并且只想再次验证登录时,如果模型设置了
ìd
使用
类型
选项
避免
放置
补丁
请求,前提是主干网确定它是更新而不是创建调用

this.model.save(null, { type: 'POST' });
传递给和的选项(所有这些选项都使用)也传递给函数

现实的解决方案 首先

然后,在
会话
模型中创建一个函数来处理登录。您甚至可以完全避免主干REST函数,因为它并不真正适合登录请求的用例

使用模型很有用,因为它提供了常见的主干事件,并且与插件(如登录表单视图中的双向绑定)配合使用也很好。但是调用
save
登录并不清楚它应该做什么。这就是为什么我们应该为
会话
模型提供一个清晰的API

var Session = Backbone.Model.extend({
    urlRoot: "http://example.com/reddit/index.php/welcome/sessions",
    defaults: {
        username: null,
        password: null
    },

    // hide the login complexity inside a function
    login: function(options) {
        // for a really simple login, this would be enough
        return this.save(null, { type: 'POST' });

        // for anything more complex, make a custom call.
        return Backbone.ajax(_.extend({
            url: this.url(),
            method: "POST",
            data: this.attributes,
            dataType: "json",
        }, options));
    },

    // provide other functions for a clear and simple to use API.
    logout: function(){ /*...*/ },
    isAuthenticated: function(){ /*...*/ }
});
然后登录:

var session = new Session();

// in the view
render: function() {
    // caching jQuery object and uses the view's `$` function to scope
    // the search to the view element only.
    this.$username = this.$("input#username");
    this.$password = this.$("input#password");
    return this;
},
getdata: function(){
    session.set({
        username: this.$username.val(),
        password: this.$password.val(),
    });
    session.login({
        context: this,
        success: this.onLoginSuccess
    });
}
我个人使用该插件在
localStorage
中保存auth
token
。它提供了一个现成的API,并使用
save
fetch
localStorage
同步

为什么要自定义
ajax
调用? 主干网提供了
save
以根据REST原则将
属性与服务器同步,这为不需要ID或PUT/PATCH请求的
登录
服务增加了大量开销

你最终常常会和脊梁骨作战

更多信息

您正在使用
this.model.fetch()用于检索数据。默认情况下,它发出GET请求,不发送正文或查询字符串中的任何数据

尝试查找选项和函数时,请使用。这本书也很短,很容易理解

快速修复 使用保存

this.model.save();
若要强制执行POST请求,例如当您已经登录并且只想再次验证登录时,如果模型设置了
ìd
使用
类型
选项
避免
放置
补丁
请求,前提是主干网确定它是更新而不是创建调用

this.model.save(null, { type: 'POST' });
传递给和的选项(所有这些选项都使用)也传递给函数

现实的解决方案 首先

然后,在
会话
模型中创建一个函数来处理登录。您甚至可以完全避免主干REST函数,因为它并不真正适合登录请求的用例

使用模型很有用,因为它提供了常见的主干事件,并且与插件(如登录表单视图中的双向绑定)配合使用也很好。但是调用
save
登录并不清楚它应该做什么。这就是为什么我们应该为
会话
模型提供一个清晰的API

var Session = Backbone.Model.extend({
    urlRoot: "http://example.com/reddit/index.php/welcome/sessions",
    defaults: {
        username: null,
        password: null
    },

    // hide the login complexity inside a function
    login: function(options) {
        // for a really simple login, this would be enough
        return this.save(null, { type: 'POST' });

        // for anything more complex, make a custom call.
        return Backbone.ajax(_.extend({
            url: this.url(),
            method: "POST",
            data: this.attributes,
            dataType: "json",
        }, options));
    },

    // provide other functions for a clear and simple to use API.
    logout: function(){ /*...*/ },
    isAuthenticated: function(){ /*...*/ }
});
然后登录:

var session = new Session();

// in the view
render: function() {
    // caching jQuery object and uses the view's `$` function to scope
    // the search to the view element only.
    this.$username = this.$("input#username");
    this.$password = this.$("input#password");
    return this;
},
getdata: function(){
    session.set({
        username: this.$username.val(),
        password: this.$password.val(),
    });
    session.login({
        context: this,
        success: this.onLoginSuccess
    });
}
我个人使用该插件在
localStorage
中保存auth
token
。它提供了一个现成的API,并使用
save
fetch
localStorage
同步

为什么要自定义
ajax
调用? 主干网提供了
save
以根据REST原则将
属性与服务器同步,这为不需要ID或PUT/PATCH请求的
登录
服务增加了大量开销

你最终常常会和脊梁骨作战

更多信息

您在哪里看到主干vi上有一个
类型
属性