yii2:-如何管理yii2中的angularjs发布请求 [英] yii2:- how can i manage angularjs post request in yii2

查看:168
本文介绍了yii2:-如何管理yii2中的angularjs发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将角度请求从角度控制器发送到yii前端控制器.

I am trying to send post request from angular controller to yii front end controller.

这是我的 controller.js

 'use strict';

define(['angular', 'd3', 'd3Cloud', 'services'], function (angular, d3) {
/* Controllers */
return angular.module('oc.controllers', ['oc.services'])
    .controller('InitCtrl', ['$scope','$http', function ($scope,$http) {


          $scope.loginFun = function() {

            var formData = {username:$scope.txtUserName, pwd:$scope.txtPassword};
            $http.post("http://localhost/yii2-angular-seed-master/frontend/web/site/login",formData).success(function(data){
              alert(data);
            });

          }             

      $scope.promos=[];
          var requestPromo = $http.get('http://localhost/yii2-angular-seed-master/frontend/web/category/promos');
          requestPromo.success(function( data ) {
            $scope.promos=data;
          });
    }])

    .config(['$httpProvider', function ($httpProvider) {
         $httpProvider.defaults.headers.post['X-CSRF-Token'] = $('meta[name="csrf-token"]').attr("content");
         $httpProvider.defaults.headers.common['Accept'] = 'application/json, text/javascript';
         $httpProvider.defaults.headers.common['Content-Type'] = 'application/json; charset=UTF-8';
    }])


 });

这是我的yii2前端 controller .

This is my yii2 frontend controller.

 use Yii;

 class SiteController extends Controller
 {
     public function behaviors()
     {
          return [
            'access' => [
            'class' => AccessControl::className(),
            'only' => ['logout', 'signup'],
            'rules' => [
                [
                    'actions' => ['signup'],
                    'allow' => true,
                    'roles' => ['?'],
                ],
                [
                    'actions' => ['logout'],
                    'allow' => true,
                    'roles' => ['@'],
                ],
            ],
        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'logout' => ['post'],
            ],
        ],
    ];
}

/**
 * @inheritdoc
 */
public function actions()
{
    return [
        'error' => [
            'class' => 'yii\web\ErrorAction',
        ],
        'captcha' => [
            'class' => 'yii\captcha\CaptchaAction',
            'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
        ],
    ];
}

public function actionIndex()
{
    return $this->render('index');
}


public function actionLogin()
{
// How to handle post request here?
// which code i have to use here?
// My angular get request is successfully work.
// But Post request not work. How can i manage it?
}




public function actionLogout()
{
    Yii::$app->user->logout();

    return $this->goHome();
}

}

我在Firebug中收到此错误.

I am getting this error in Firebug.

POST http://localhost/yii2-angular-seed-master/frontend/web/site/login 400错误的请求

POST http://localhost/yii2-angular-seed-master/frontend/web/site/login 400 Bad Request

"NetworkError:400错误请求- http://localhost/yii2 -angular-seed-master/frontend/web/site/login "

"NetworkError: 400 Bad Request - http://localhost/yii2-angular-seed-master/frontend/web/site/login"

请为我建议一个代码:

public function actionLogin()
{
   // How to handle post request here?
  // which code i have to use here?
  // My angular get request is successfully work.
  // But Post request not work. How can i manage it?
}

推荐答案

未发送CSRF或GET请求的某些属性时,发送代码为400的响应.可能是您需要将$ httpProvider修复为此:

Response with code 400, sends when you do not have sent CSRF or some attributes to GET request. May be you need to fix your $httpProvider to this:

angular.module('app').config(appconfig);
appconfig.$inject = ['$httpProvider'];
function appconfig($httpProvider){
    $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
    $httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name="csrf-token"]').attr('content');
}

这篇关于yii2:-如何管理yii2中的angularjs发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆