How do I POST urlencoded form data with $http without jQuery?

后端 未结 11 2405
生来不讨喜
生来不讨喜 2020-11-21 23:27

I am new to AngularJS, and for a start, I thought to develop a new application using only AngularJS.

I am trying to make an AJAX call to the server side, using

11条回答
  •  無奈伤痛
    2020-11-22 00:25

    This worked for me. I use angular for front-end and laravel php for back-end. In my project, angular web sends json data to laravel back-end.

    This is my angular controller.

    var angularJsApp= angular.module('angularJsApp',[]);
    angularJsApp.controller('MainCtrl', function ($scope ,$http) {
    
        $scope.userName ="Victoria";
        $scope.password ="password"
    
    
           $http({
                method :'POST',
                url:'http://api.mywebsite.com.localhost/httpTest?callback=JSON_CALLBACK',
                data: { username :  $scope.userName , password: $scope.password},
                headers: {'Content-Type': 'application/json'}
            }).success(function (data, status, headers, config) {
                console.log('status',status);
                console.log('data',status);
                console.log('headers',status);
            });
    
    });
    

    This is my php back-end laravel controller.

    public function httpTest(){
            if (Input::has('username')) {
                $user =Input::all();
                return  Response::json($user)->setCallback(Input::get('callback'));
            }
        }
    

    This is my laravel routing

    Route::post('httpTest','HttpTestController@httpTest');
    

    The result in browser is

    status 200
    data JSON_CALLBACK({"username":"Victoria","password":"password","callback":"JSON_CALLBACK"}); httpTesting.js:18 headers function (c){a||(a=sc(b));return c?a[K(c)]||null:a}

    There is chrome extension called postman. You can use to test your back-end url whether it is working or not. https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en

    hopefully, my answer will help you.

提交回复
热议问题