-1

I am trying to make a simple POST request for sign in but it doesn't work. I read this thread (AngularJs $http.post() does not send data) and tried almost every suggestion but nothing works. Here is my code:

$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';

    $http({
        "async": true,
        "crossDomain": true,
        "url": "https://myurl/api_v1/login/json",
        "method": "POST",
        "headers": {"Content-Type": "application/x-www-form-urlencoded"},
        data: {
            "username": "admin@admin.adm",
            "password": "admin"
        }

    }).then(function successCallback(response) {

        console.log(response);

    }, function errorCallback(response) {

        alert(response);
    });
Community
  • 1
  • 1
charbinary
  • 1,875
  • 4
  • 17
  • 25
  • 1
    Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://myurl/api_v1/login/json. (Reason: CORS header 'Access-Control-Allow-Origin' missing). – charbinary Mar 07 '16 at 09:40
  • Raja Sekar, if you want you can add your comment as an answer so I can rate it. After I saw the browser error I found a solution at http://blog.ionic.io/handling-cors-issues-in-ionic/. Thank you! – charbinary Mar 07 '16 at 09:51
  • thanks!! but it is not an answer. – Raja Sekar Mar 07 '16 at 10:05
  • Yes, but it's point me to right way. – charbinary Mar 07 '16 at 10:09

3 Answers3

0

Hello Try This Full Example

 $scope.sendMessage = function() {

    var userSendingText = $('#userSendingText').val();

          var urlString1 = "http://XYZ...............";
          $('#userSendingText').val('');
          console.log(urlString1);
          $.ajax({
            type: "POST",
            url: urlString1,
            cache: false,
            success: function(result) {
            }
          });
  };
0

As stated in the comments, the problem is CORS. For "ionic serve" you can use this chrome plugin: https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi and my code works just fine without any changes.

charbinary
  • 1,875
  • 4
  • 17
  • 25
-1
$http.post("REST_API_URL",data).success(function (data) {
                //Perform when success  

                 }).error(function (data, status, headers, config) {
                  //Perform when error  

                 });
  • The reason is because Cross-Origin Request Blocked. – charbinary Mar 07 '16 at 09:52
  • The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error. – Satej S Mar 07 '16 at 09:55