0

I am using AngularJS to make calls to a remote unit that supports JSON API. The call is proceeded as this:

'use strict';
var app=angular.module('app', [])
app.controller('Login', function ($scope, $http) {
    $scope.login = function () {
        $scope.user;
        $scope.passwd = "";
        $scope.message = "";
        $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
        $http.post('http://192.168.30.1/jsonrpc',
            {
                "params": [{
                    "url": "sys/login/user",
                    "data": [{
                        "passwd": $scope.passwd,
                        "user": $scope.user,
                    }]
                }],
                "session": 1,
                "id": 1,
                "method": "exec"
            }).then(function (response) {
                if (response.result.status.code == 0) {
                    $scope.message = 'Bienvenue'
                }
                else {
                    $scope.message = 'Non authetifie'
                }
            }, function () {
                $scope.message = 'erreur'
            })

    }
} );

As shown in the code above, The second argument in $http.post is some JSON data which will be sent to the remote unit and the response is also JSON.

When I send a request I can see the JSON response with fiddler but in the $scope.message I get erreur since for the javascript no response was received.

Since I am falling in the CORS problem, I want to know can I send this same data as JSONP using $http.jsonp?

The reason for my question is that $http.jsonp only takes the url and [config] as parameters unlike $http.post so I can't send my JSON data with it.

  • http://stackoverflow.com/questions/28677773/my-client-side-cors-really-not-working-in-angular-controller-pls-help-me-tim – Prasad Mar 02 '16 at 14:54
  • I don't think you can use jsonp like that. have a look at http://stackoverflow.com/questions/4508198/how-to-use-type-post-in-jsonp-ajax-call – KnuturO Mar 02 '16 at 14:56
  • Yep i admit deleted but just posted documentations – Prasad Mar 02 '16 at 14:59
  • Unless you control the API and can enable CORS you will need to use a proxy since jsonp does not support POST or request headers ... it is a script request – charlietfl Mar 02 '16 at 15:00
  • @charlietfl What is wrong with client side cors Post does it not worth a read ???? – Prasad Mar 02 '16 at 15:02
  • @N.V.Prasad all the documentation you're giving me says that using `$http.jsonp` only take the `url` as parameter. My question is how can I specify the json data to be sent in my request using this function? – Yacine Ben Slimene Mar 02 '16 at 15:04
  • @YacineBenSlimene you can't post period. Any data needs to be in a GET query string so you can pass params in `$http` config object. Also not all API's serve jsonp and you can't force it if they don't – charlietfl Mar 02 '16 at 15:09
  • @YacineBenSlimene - what is "the CORS problem" that you face? Is the remote unit under your control? Can you implement CORS? – Starscream1984 Mar 02 '16 at 15:13
  • @Starscream1984 The remote unit is a FortiOS equipment, so I can't enable CROS on it. By the way, when I send a request I can see the JSON response with fiddler but in the `$scope.message` I get erreur since for the javascript no response was received. I will add this remarque to the question. Sorry – Yacine Ben Slimene Mar 02 '16 at 15:17
  • @YacineBenSlimene - if enabling CORS is impossible, but you *can* configure the endpoint to serve JSONP - you'll have to also modify the endpoint to be a GET and pass all your data in the query string, as charlietfl suggests. – Starscream1984 Mar 02 '16 at 15:22
  • @Starscream1984 So I can't use `JSONP` to post data ? There is no way around this issue ? – Yacine Ben Slimene Mar 02 '16 at 15:25
  • @YacineBenSlimene - there is an answer on this question: http://stackoverflow.com/questions/5345493/using-put-post-delete-with-jsonp-and-jquery that claims to achieve POST via JSONP. But I wouldn't like to try those complicated and unproven steps, it's much, much, much easier to change the endpoint to be a GET whilst you configure it to serve JSONP. – Starscream1984 Mar 02 '16 at 15:35
  • Can't you use a proxy that you do control?...that's usually the easiest workaround – charlietfl Mar 02 '16 at 16:19

0 Answers0