2

I want to make a rest call to server using Angular but I am block I tried with traditional jQuery ajax call with Basic Authorization header looks something like this:

        jQuery.ajax({
            url: "http://localhost:8080/public/emp",
            type: "GET",
            dataType:"jsonp",
            Accept : "application/json",
            contentType: "application/json",
            headers: {
                'Authorization': 'Basic XXXXXXXXXXX'
            },
            success: function (data){
                alert(data);
            },
            error: function(){
                alert('Error');
            }
        });

It works perfectly and Data is retrieved.

But When I try to convert it with $http of AngularJs. Something like this:

        $http({
            method:'GET',
            url : 'http://localhost:8080/public/emp',
            headers: {'Authorization': 'Basic XXXXXXXXXX',"content-type":"application/json","Accept" : "application/json"}})
        .success(function (data) {
            alert("success");
        })
        .error(function (error) {
            alert("error");
        });

Its giving me Error 401 - Unauthorized

How can I add dataType property to $http? I have tried this link of stack overflow but Still unable to get a solution.

0

3 Answers 3

1

Did you try $http.jsonp method? For your case this should work like this:

$http.jsonp('http://localhost:8080/public/emp', {
        headers: {
            'Authorization': 'Basic XXXXXXXXXX',
            "content-type":"application/json",
            "Accept" : "application/json"
        }});

Also here is working plunker example. And more general one from angular team

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, The call completed but Its callbacked in error response. Moreover this being a REST call how can I mention it is a GET request so that server responds correctly ?
I got it working and data is also retrieved by adding third parameter data-type somethin like this: [{'data-type':"json"}] but I am getting the callback in error function.
@user3256761 this is already GET request. Here is example for both Angular and jQuery jsonp requests. You can see that the only difference they have is the callback function. If you can craft a sample this could help.
0

Try this get method

In js

$http({
                url: 'customUrl',
                method: 'GET',
                params: {},
            }).then(function (result) {
                return result;
            }).catch(function (e) {
                throw e;
            });

In api

[HttpGet]
    public string GetDraftData(byte id)
    {
        return string;
    }

Try this post method

In js

                $http({
                    url: 'customUrl',
                    method: 'POST',
                    data: yourJsonData,
                    headers: { 'Content-Type': 'application/json; charset=UTF-8' },
                    //params: { jsonData: "I am shohel rana" },
                    //timeout: 10,
                    //cache: false,
                    //transformRequest: false,
                    //transformResponse: false
                }).then(function (success) {
                    return success;
                }).catch(function (e) {
                    throw e;
                });

In api

       [HttpPost]
        public bool SaveDraft([FromBody]object draft)
        {
            try
            {
                return _iDraftDataSvc.InsertDraftData(draft);
            }
            catch (Exception)
            {
                throw;
            }
        }

Comments

0

Try changing the method from GET to JSONP. I'm not quite sure if it's possible to add custom headers though (as I've heard), I haven't tried that yet.

$http({
    method:'JSONP',
    url : 'http://localhost:8080/public/emp',
    headers: {'Authorization': 'Basic XXXXXXXXXX',"content-type":"application/json","Accept" : "application/json"}})
.success(function (data) {
    alert("success");
})
.error(function (error) {
    alert("error");
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.