0

I want to receive json from this API but it has authentication which username(nama pengguna) is "foo" and password(sandi) "foo". How I can receive it in my program? I use Jquery and eant build it with phonegap and build for Android

I am trying like this

$.ajax({
                url: 'http://api.tabloidnova.com/1/subscribe/get?api_key=259225f04f4015746b03e1bad6238eaa&format=json&channel_id=110',
                beforeSend: function(xhr){
                    xhr.setRequestHeader("Authorization","Basic foo:foo");
                },
                dataType: 'json',
                async: false,
                success: function(data){
                    $.each(data,function(i,grab){
                        console.log(grab[i].article.node_id);
                        console.log("tes");
                    })
                }
            })

but in my ripple emulator it still give me unauthorized error. If I use without ripple emulator it can't work because I run it from localhost which has origin policy. Any suggestion?

SOLVED now. Finally I encode it my username and password first. Thanks

albilaga
  • 419
  • 1
  • 10
  • 26
  • possible duplicate of [How to use Basic Auth and Jquery and Ajax](http://stackoverflow.com/questions/5507234/how-to-use-basic-auth-and-jquery-and-ajax) – Shiva Jan 02 '14 at 12:45

2 Answers2

0

In javascript, with ajax we can do this way:

$.ajax({
    'url': 'http://host.com/action/',
    'beforeSend': function(xhr) {
        xhr.setRequestHeader("Authentication", "Basic " + encodeBase64(username + ":" + password) //May need to use "Authorization" instead
    },
    sucess: function(result) {
        alert('done');
    }
});

Check the documentation for jQuery.ajax(). You will have more options to implement this.

Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
0

You will need to set the appropriate request header to pass the credentials. See for example here.

$.getJSON({
    'url': 'http://host.com/action/',
    'otherSettings': 'othervalues',
    'beforeSend': function(xhr) {
        //May need to use "Authorization" instead
        xhr.setRequestHeader("Authentication",
            "Basic " + encodeBase64(username + ":" + password)
    },
    sucess: function(result) {
        alert('Done');
    }
});
Suhas Gosavi
  • 2,170
  • 19
  • 40