从Backbone到Laravel的Ajax发布请求

时间:2013-09-28 12:58:33

标签: ajax backbone.js laravel

我正在尝试使用Ajax请求向Laravel发送Backbone集合。 我不需要保存它或更新数据库我只需要使用Omnypay php Api处理数据。不幸的是,Laravel Controller变量$ input = Input :: all()包含一个空字符串。

    var url = 'index.php/pay';
    var items = this.collection.toJSON;      

    $.ajax({
        url:url,
        type:'POST',
        dataType:"json",
        data: items,
        success:function (data) {             
        if(data.error) {  // If there is an error, show the error messages
                $('.alert-error').text(data.error.text).show();
            }            
        }
    });

这是Laravel路线:

Route::post('pay','PaypalController@doPay');

最后是Laravel控制器:

class PaypalController extends BaseController {

public function doPay() {

        $input=Input::all();
    }
  }

2 个答案:

答案 0 :(得分:0)

您的route无法匹配,

Route::post('pay','PaypalController@doPay');

所以网址应该是

var url = 'pay';

而不是

var url = 'index.php/pay';
顺便说一下,不确定是否还有其他问题(backnone)是错误的。

更新: toJSON是一种方法,因此它应该是(您错过()

var items = this.collection.toJSON();

答案 1 :(得分:0)

我发现将主干集合转移到Laravel的黑客解决方案是将集合转换为JSON,然后将其包装在适合jQuery Ajax POST的普通对象中。这是代码:

var url = 'index.php/pay';
var items = this.collection.toJSON();
var plainObject= {'obj': items}; 

$.ajax({
    url:url,
    type:'POST',
    dataType:"json",
    data: plainObject,
    success:function (data) {             
    if(data.error) {  // If there is an error, show the error messages
            $('.alert-error').text(data.error.text).show();
        }            
    }
});

现在我的“doPay”控制器函数的$ input变量包含一个Backbone模型数组。