$http.post() method is actally sending a GET

后端 未结 2 762
庸人自扰
庸人自扰 2020-12-20 14:16

NOTE:

I\'ve found a possibly related issue that warrants a new question here


This is a weird problem. I\'ve been using angular over the course of 2 y

相关标签:
2条回答
  • 2020-12-20 14:49

    This is due to a security consideration.

    In your situation when a redirect is sent back from the server to the browser, the browser will not repeat the POST request (but rather just a "simple" GET request).

    Generally speaking a browser will not send POST data to a redirect URL because the browser is not qualified to decide if you're willing to send the same data to the new URL what you intended to send to the original URL (think about passwords, credit card numbers and other sensitive data). But don't try to circumvent it, simply use registered path of your handler to POST to, or any of the other tips mentioned in the linked answer.

    For context see question:

    Go web server is automatically redirecting POST requests

    You can read more on the subject here:

    Why doesn't HTTP have POST redirect?

    0 讨论(0)
  • 2020-12-20 15:09

    This code actually send GET to server

    $http({
                method: 'POST',            
                params: {
                    LoginForm_Login: userData.username,
                    LoginForm_Password: userData.password
                },
                url: YOURURL
            }).then(
    

    You need to use transformRequest, sample below actually send POST

    $http({
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                transformRequest: function (obj) {
                    var str = [];
                    for (var p in obj)
                        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                    return str.join("&");
                },
                data: {
                    LoginForm_Login: userData.username,
                    LoginForm_Password: userData.password
                },
                url: YOURURL
            }).then(
    
    0 讨论(0)
提交回复
热议问题