12-02-2014 11:53 PM
Hi,
I want to update one of the call variables in dialog object by making a REST webservice request (PUT), and embedded the Basic Credentials in the webservice request.
Can I get an example how to implement Basic Credentials when making REST webservice call?
Thanks in advance!
Best Regards,
Yudhi
Solved! Go to Solution.
12-03-2014 11:29 AM
Hi Yudhi,
Basic authentication is simple with jQuery. Just add this to your $.ajax request.
headers: { | ||||||
"Authorization": "Basic " + btoa(USERNAME + ":" + PASSWORD) | ||||||
}, |
For example:
$.ajax({ | ||||||
type: 'PUT', | ||||||
url: 'some-url', | ||||||
headers: { | ||||||
"Authorization": "Basic " + btoa(USERNAME + ":" + PASSWORD) | ||||||
}, | ||||||
dataType: 'xml', | ||||||
data: 'some-xml-data', | ||||||
success: function (){ | ||||||
alert('Success!'); | ||||||
} | ||||||
}); |
Keep in mind that if you're using jQuery (or any other AJAX method) to do a REST PUT operation, you have to take CORS into account. Your $.ajax URL has to be on the same server as the web application you're writing. Otherwise you'll get an error like this: XMLHttpRequest cannot load [URL]. No 'Access-Control-Allow-Origin' header is present on the requested resource.
Unfortunately, there's no easy answer to that problem.
Thanks,
Nick
12-03-2014 08:11 AM
Are you talking about Basic Authentication (base64 encoded username:password)? If so, what language are you using to perform the REST operation?
12-03-2014 09:44 AM
Hi Nicholas,
Thanks for your response.
Yes, I'm talking about Basic Authentication (base64 encoded username:password). I'm using JavaScript for generate the REST request and perform the REST operation using jQuery (send Ajax request to backend).
Thanks,
Yudhi
12-03-2014 11:29 AM
Hi Yudhi,
Basic authentication is simple with jQuery. Just add this to your $.ajax request.
headers: { | ||||||
"Authorization": "Basic " + btoa(USERNAME + ":" + PASSWORD) | ||||||
}, |
For example:
$.ajax({ | ||||||
type: 'PUT', | ||||||
url: 'some-url', | ||||||
headers: { | ||||||
"Authorization": "Basic " + btoa(USERNAME + ":" + PASSWORD) | ||||||
}, | ||||||
dataType: 'xml', | ||||||
data: 'some-xml-data', | ||||||
success: function (){ | ||||||
alert('Success!'); | ||||||
} | ||||||
}); |
Keep in mind that if you're using jQuery (or any other AJAX method) to do a REST PUT operation, you have to take CORS into account. Your $.ajax URL has to be on the same server as the web application you're writing. Otherwise you'll get an error like this: XMLHttpRequest cannot load [URL]. No 'Access-Control-Allow-Origin' header is present on the requested resource.
Unfortunately, there's no easy answer to that problem.
Thanks,
Nick
12-03-2014 06:18 PM
Hi Nick,
Thank you for your help! It's really helpful!
Best Regards,
Yudhi
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide