This answer has helped me largely but hasn’t fully resolved my issues. I’m using code similar to that, which is:
function getResponse() {
var the_url = "http://www.myurl.com/api/get";
$.ajax({
url: the_url,
dataType: 'jsonp',
type: 'get',
success: function(data) {
var json_response = data;
alert(data);
}
});
}
Basically, instead of just posting a url I need to post a curl because of authentication.
The example works fine with the simple url, but what I am wanting to use for ‘the_url’ is this:
var the_url = 'curl -u username:password "http://www.myurl.com/api/get"';
This is where I am having issues which means the success statement is never reached.
What is the best way to resolve this, and if it’s not achievable could someone recommend the best workaround?
Thanks in advance.
If you were using simple AJAX, then you could just add custom (basic authentication) header at the begining of your AJAX call. But you are using JSONP, which actually is equivalent to adding
<script>tag to your HTML.So what you are asking is: can I do basic authentication with
<script>tag? No, you can’t.The only possibility left for you is to do server side processing, like Matt Gibson suggested.
Note however, that the browser should fire authentication dialog after the JSONP request (also see this answer). Isn’t it working in your case??