简体   繁体   English

Backbonejs模型的.save()的PUT和POST请求

[英]PUT and POST Request of .save() of Backbonejs model

How to be confirmed whether a backbonejs .save() is sending PUT request ?? 如何确认是否.save()正在发送PUT请求? I checked my server side, which is working good, there is no problem in server side. 我检查了服务器端,它工作正常,服务器端没有问题。 But my .save() is not working. 但是我的.save()无法正常工作。

Here is my model of backbone 这是我的骨干模型

define(['underscore','backbone'],function(_,Backbone) 
{
    var my_model = Backbone.Model.extend(
    { 
        urlRoot: "http://localhost/back/server_file.php/number"
    });         
    return my_model;
});

Here is how I am using .save() 这是我使用.save()

var my_data = {
            id: data.id, 
            code: data.code
        };   

        var My_model = new my_model();  

        My_model.save(my_data, 
        {
            success: function(response) 
            {
                alert('Yes');

            },
            error: function(response)
            {
                alert('No');
            }
        });  

I think my .save() is sending POST request to server. 我认为我的.save()正在向服务器发送POST请求。

UPDATE UPDATE

I think I could find out my problem. 我想我可以找出我的问题。 I am describing that here. 我在这里描述。

What I would like to do 我想做什么

I would like to send 2 parameters from backbonejs model to server side script (I am using PHP SLIM Framework). 我想将2个参数从elebonejs模型发送到服务器端脚本(我正在使用PHP SLIM Framework)。 Based on those 2 parameters server side script update a record's(2 field of this record match with those 2 parameters ) another field with a static parameter at database. 基于这两个参数,服务器端脚本会更新数据库中具有静态参数的记录(该记录的这两个字段与这两个参数匹配)的另一个字段。

What backbonejs provide (As I think ) 什么是elegatejs提供的 (我认为)

Backbonejs has a model with id as JSON format. Backbonejs有一个ID为JSON格式的模型。 Backbonejs sends PUT request to server side script. Backbonejs将PUT请求发送到服务器端脚本。 Server side script just dump (update) the data(which was as JSON format,like a bundle) to the database with matching id. 服务器端脚本只需将ID匹配的数据(JSON格式,如捆绑包)转储(更新)到数据库。 Serer side script would not like to look inside the data. Serer侧脚本不想查看数据内部。

I am getting (from network tab of firebug) my PUT request URL is like http://localhost/back/server_file.php/number/1 (This is the id) . 我从Firebug的网络选项卡中获取我的PUT请求URL就像http://localhost/back/server_file.php/number/1 (This is the id) On the other hand I would like to get URL is like http://localhost/back/server_file.php/number/1 (id the first parameter)/456 (Second parameter) . 另一方面,我想获取URL就像http://localhost/back/server_file.php/number/1 (id the first parameter)/456 (Second parameter)

If I am right, anyone could say how can I implement my plan?? 如果我是对的,那么任何人都可以说我该如何执行我的计划?

This should work, 这应该工作,

My_model.set(my_data);

My_model.save(null, {
  wait : true,
  url : "http://localhost/back/server_file.php/number/1/456",
  success : function(response){
  },
  error : function(e){
  }
});

You can debug the request being sent in network tab of Chrome Developer Tools or you can use a network tool like Fiddler to see all requests. 您可以在Chrome Developer Tools network标签中调试正在发送的请求,也可以使用Fiddler类的网络工具查看所有请求。

Refer the attached on where to see the request method being used. 请参阅附件,以查看正在使用的请求方法。

在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM