Angular.js的POST错误 [英] POST error with Angular.js
问题描述
我目前正在尝试使用laravel和angular.js建立一个威士忌的网上商店。我试图简单地添加一个产品到我的篮子,但我在控制台中仍然有相同的错误。我想把我的product_id和我的basket_id在'basket_product'列。现在我想把product_id放在id为3的篮子里。
我已经在这个问题上呆了几天, 。
这是我的' BasketController.php '
class BasketController extends Controller
{
public function index()
{
return Basket :: join('basket_product','baskets .id','=','basket_product.basket_id') - > get();
}
public function create()
{
}
public function store $ b {
$ rules = [
'basket_id'=> 'required | integer',
'product_id'=> 'required | integer',
];
$ this-> validate($ request,$ rules);
$ basket = new Basket($ request-> only(['basket_id','product_id']));
if($ basket-> save()){
return response()
- > json($ basket)
- > setStatusCode :: HTTP_CREATED);
}
}
public function show()
{
}
}
这是我的' product.controller.js '
vm。$$ ix = {
save:createBasketItemAction
};
vm.item = {
basket_id:3,
products_id:$ state.params.id
};
switch($ state.current.name){
case'products.grid':
vm.products = getProductsAction();
vm.categories = getCategoryAction();
break;
case'products.show':
vm.categories = getCategoryAction();
vm.product = getProductAction();
vm。$$ ix.save = createBasketItemAction;
break;
默认值:
break;
}
function createBasketItemAction(){
$ log.info(createBasketItemAction.name,'post:',vm.item);
return BasketResource
.save(vm.item,success,error);
函数错误(错误){
$ log.error(createBasketItemAction.name,'ERROR','error:',error);
}
function success(resource,responseHeader){
$ log.log(createBasketItemAction.name,'SUCCESS','resource:',resource,'responseHeader:', responseHeader());
$ state.go('blog.posts');
}
}
这是我用来触发在我的 product.view.html 中发布后事件
< md-button ng-click =vm。$$ ix.save()ng-disabled =postForm。$ invalid>添加到购物篮< / md-button>
最后,这是我的' basket-resource.service.js '
;(function(){
'use strict';
angular。模块('app.basket')
.factory('BasketResource',BasketResource);
/ * @ngInject * /
function BasketResource(
// Angular
$ cacheFactory,
$ resource,
// Custom
UriService
){
var url = UriService.getApi('basket_products');
var paramDefaults = {
};
var actions = {
'get':{
method :'GET',
isArray:true
},
};
return $ resource(url,actions);
}
})();
这是我的数据库的构建方式:
提前感谢!
我想你正在做一个XMLHttpRequest到不同的域,而不是你的页面。所以浏览器阻塞它,因为它通常允许同一来源的请求出于安全原因。当您想要执行跨网域请求时,您需要执行不同的操作。有关如何使用laravel启用CORS的教程: http://en.vedovelli.com.br/2015/web-development/Laravel-5-1-enable-CORS/
I'm currently trying to build a webshop for whisky using laravel and angular.js. I was trying to simply add a product to my basket but I keep having the same errors in my console. I wanted to just put my product_id and my basket_id in the 'basket_product' column. For now I want to put the product_id in the basket with id 3.
I've been stuck on this issue for days now and I just can't seem to figure it out.
these are the errors:
This is my 'BasketController.php'
class BasketController extends Controller
{
public function index()
{
return Basket::join('basket_product', 'baskets.id', '=', 'basket_product.basket_id')->get();
}
public function create()
{
}
public function store(Request $request)
{
$rules = [
'basket_id' => 'required|integer',
'product_id' => 'required|integer',
];
$this->validate($request, $rules);
$basket = new Basket($request->only(['basket_id', 'product_id']));
if ($basket->save()) {
return response()
->json($basket)
->setStatusCode(Response::HTTP_CREATED);
}
}
public function show()
{
}
}
This is my 'product.controller.js'
vm.$$ix = {
save : createBasketItemAction
};
vm.item = {
basket_id: 3,
products_id: $state.params.id
};
switch ($state.current.name) {
case 'products.grid':
vm.products = getProductsAction();
vm.categories = getCategoryAction();
break;
case 'products.show':
vm.categories = getCategoryAction();
vm.product = getProductAction();
vm.$$ix.save = createBasketItemAction;
break;
default:
break;
}
function createBasketItemAction() {
$log.info(createBasketItemAction.name, 'post:', vm.item);
return BasketResource
.save(vm.item, success, error);
function error(error) {
$log.error(createBasketItemAction.name, 'ERROR', 'error:', error);
}
function success(resource, responseHeader) {
$log.log(createBasketItemAction.name, 'SUCCESS', 'resource:', resource, 'responseHeader:', responseHeader());
$state.go('blog.posts');
}
}
This is the line of code I used to fire the post event in my 'product.view.html'
<md-button ng-click="vm.$$ix.save()" ng-disabled="postForm.$invalid">Add to Basket</md-button>
And finally, this is my 'basket-resource.service.js'
;(function () {
'use strict';
angular.module('app.basket')
.factory('BasketResource', BasketResource);
/* @ngInject */
function BasketResource(
// Angular
$cacheFactory,
$resource,
// Custom
UriService
) {
var url = UriService.getApi('basket_products');
var paramDefaults = {
};
var actions = {
'get': {
method: 'GET',
isArray: true
},
};
return $resource(url, actions);
}
})();
This is how my database is built:
thanks in advance!
I think you are doing an XMLHttpRequest to a different domain than your page is on. So the browser is blocking it as it usually allows a request in the same origin for security reasons. You need to do something different when you want to do a cross-domain request. A tutorial about how to enable the CORS with laravel : http://en.vedovelli.com.br/2015/web-development/Laravel-5-1-enable-CORS/
这篇关于Angular.js的POST错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!