使用angularjs将数据发布到服务器

时间:2017-11-01 10:39:00

标签: angularjs

这对我有用

// default post header
    $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
    // send login data
    $http({
        method: 'POST',
        url: 'abc.php',
        data: $.param({
            email: "abc@gmail.com",
            password: "password"
        }),
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).then(function (data, status, headers, config) {
        // handle success things
    }).catch(function (data, status, headers, config) {
        // handle error things
    });

这不是

$http.post('abc.php', {email: "abc@gmail.com",
            password: "password"})
        .then(function(res){
            $scope.response = res.data; 
    })

嗨,你可以讨好为什么第一次实施第二次没有工作。我对短切和长切角方法很困惑 提前致谢

2 个答案:

答案 0 :(得分:0)

从根本上说,问题在于您选择的服务器语言本身无法理解AngularJS的传输。

Content-Type: application/json正在将您的数据传输为$http.post("abc.php", requestData, { headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}, transformRequest: transform }).success(function(responseData) { //do stuff with response });

将标题更改为URL编码如下: -

const int WAIT_PRECISION_MS = 10;  // Change it to whatever you like
int TIME_TO_WAIT_MS = 2000;        // Change it to whatever you like
int ms_waited = 0;
bool got_lock = false;
while (ms_waited < TIME_TO_WAIT_MS) {
    std::this_thread::sleep_for(
                             std::chrono::milliseconds(WAIT_PRECISION_MS));
    ms_waited += WAIT_PRECISION_MS;
    got_lock = YOUR_MUTEX.try_lock();
    if (got_lock) {
        break;
    }
}

答案 1 :(得分:-1)

第二个片段应该是这样的

$http.post('abc.php', {email: "abc@gmail.com",
            password: "password"})
    }).then(function(res){
            $scope.response = res.data;
 });