Authenticating to a data source

8 posts, 1 answers
  1. luke
    luke avatar
    9 posts
    Member since:
    Jun 2011

    Posted 09 May 2013 Link to this post

    Hi there

    New to Kendo

    Hoping for some direction here. When i use the following code i keep getting a 401 error. The username and password are correct
    var dataSource = new kendo.data.DataSource({
                      autoSync: true,
                        
                      transport: {
                        read:  {
                            url: "web service url",
                            beforeSend: function (xhr) {
                    //your code
                                xhr.setRequestHeader("Authorization", "Basic username:password"); //i set the username and password to valid settings
                },
                          dataType: "jsonp" // "jsonp" is required for cross-domain requests; use "json" for same-domain requests
                        },
                        update: {
                          url: "web service url",
                          dataType: "jsonp" // "jsonp" is required for cross-domain requests; use "json" for same-domain requests
                        }
                      },
                      schema: {
                        model: { id: "Id" }
                      }
    });


    If i use eclipse and build an android app i can set the username and password properties of the DefaultHttpClient and i can connect successfully, so it appears the service works (I can also access it via a browser which prompts for credentials and i get the data)

    any ideas ??

  2. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 13 May 2013 Link to this post

    Hello,

     The problem is caused by the JSONP dataType setting. jQuery doesn't use an XMLHttp request when performing JSONP requests. As a result you cannot set request headers. Try setting the dataType option to "json" if your service is in the same domain.

    Regards,
    Atanas Korchev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Kendo UI is VS 2017 Ready
  4. luke
    luke avatar
    9 posts
    Member since:
    Jun 2011

    Posted 13 May 2013 Link to this post

    Thanks for the reply Atanas

    I set the dataType option to json with no luck. The service is outside of our domain, and clients will be accessing it via the internet. Anything else you can think of ???

  5. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 13 May 2013 Link to this post

    Hi,

    Since setting the request header is supported only for json requests I cannot think of a workaround. 

    Greetings,
    Atanas Korchev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  6. Answer
    luke
    luke avatar
    9 posts
    Member since:
    Jun 2011

    Posted 14 May 2013 Link to this post

    I managed to get this working by encoding the username:password combination

    Now just make sense of the data

    Luke
  7. JonathanElsner
    JonathanElsner avatar
    20 posts
    Member since:
    Feb 2004

    Posted 03 Oct 2013 Link to this post

    Can someone give an example of this?  I am trying to authenticate to a service outside of our domain so I need to set the authentication header and use jsonp?
    If this is not possible what is the recommended way to authenticate with a service outside my domain?
  8. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 04 Oct 2013 Link to this post

    Hello Jonathan,

     Setting any HTTP headers isn't possible with JSONP. The latter works by dynamically injecting <script> elements in the page which doesn't allow setting http headers. The only suggestion we can give is to use CORS if possible.

    Regards,
    Atanas Korchev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  9. steve gray
    steve gray avatar
    14 posts
    Member since:
    Mar 2006

    Posted 15 Apr 2015 in reply to luke Link to this post

    Can you post your code, please? I'm working on this too.
Back to Top
Kendo UI is VS 2017 Ready