By: TwitterButtons.com

Recent blog posts

User login

Home | Blogs | Stephane Eyskens's blog

using jQuery with ListData.svc and redirecting the user when not logged in using FBA

Hi,

When querying ListData.svc directly from AJAX, if your SharePoint web application is configured using Windows Authentication with either NTLM or Kerberos, your browser will either transmit your credentials automatically (Internet Explorer), either will prompt you to let you logging in (other than IE).

This behavior is not similar when trying to perform AJAX queries in FBA mode, so when your SharePoint web app is set up to use FBA combined to CBA.

In that case, the following piece of code:

would branch to the error part in case you're not yet authenticated. To handle that, you can redirect the user to the regular SharePoint login page so that he gets this:


To handle that, you can use a piece of code looking like this one:

where you test the returned status which is 302 with Firefox & 12150 with IE 8 for some reason...In case this error is returned, you just redirect the user by building the new URL dynamically. SharePoint will do the remainder and the user will be redirected back to the current page after authentication gets complete.

$(document).ready(function () {
$.ajax({
type: "GET",
url: "_vti_bin/ListData.svc/Villes?$select=Id,Title",
contentType: "application/json; charset=utf-8",
accept: "application/json",
success: function (xhr) {
alert("ok");
},
error: function (xhr) {

if (xhr.status == 12150 || xhr.status == 302) {
alert('You are being redirected to the login page since you are not yet authenticated');
window.location = window.location.protocol + "//" +
window.location.host + '/_forms/default.aspx?ReturnUrl=' + window.location.pathname;
}
}
});