Forum Discussion

Telstra1's avatar
11 years ago

Connecting to LSW data in custom endpoint

Hello,

 

I'm creating an HTML custom endpoint.  I need to call LSW system with the basic authorization using Ajax to get and parse the result so I can then query the right information.  Can someone help point me in the right direction on how to do this?

 

I tried the below AJAX code,

 

$.ajax({
    url: "https://test.response.lithium.com/api/reports/csv/conversation?xslt=json.xsl",
    type: "GET",
    data: {
       startTime: '2013-01-20'
    },
    beforeSend: function (request)
    {
        request.withCredentials = true;
    request.setRequestHeader("x-user", 'user')
        request.setRequestHeader("x-pass", 'pass')
        request.setRequestHeader("Authorization", "Basic  XXXXXXXX");
    },
    crossDomain:true,
    dataType:"jsonp",
    success: function(res) {
        alert("Success   " + res);
    },
    error: function(err) {
        alert("Error   " + err);
    }
});

 

but this always returns error message -

{"result":"fail","errorMessage":"Unauthorized"}

 

the username/password is already base64 encoded, and they are correct. but still get an unauthorized error. am i doing something wrong?

 

any help appreciated.

 

Thanks,

Shalini.

  • Hi Shalini,

     

    Few things that might be of help.

    1. I would recommend you to test the RESTcall using a REST client  (chrome plugin , poster etc) , so that you would know the api , query parameters , data that should be given.
    2. I can see that you are authenticating , with a GET call , which is usually not the case. Generally , a POST call is used for authentication.
    3. Having said that , if you are doing a cross domain call , and handling it with jsonp , that would work only for GET call and not a POST call. You might have to think of an alternate solution for it. My suggestion would be , if you have control over the other end , to add the "access control allow origin" to the header of the method on server side.

     

    Here is a sample ajax POST call that worked for me.

    $.ajax({
    url: 'http://blabla.k2.com/doSomething/',
    type: 'POST',
    data:  data,
    cache: false,
    dataType: 'json',
    processData : false, // Don't process the files
    contentType: false, // Set content type to false as jQuery will tell the server its a query string request
    success: function (data) {
    console.log(data.UploadStatus);
    },
    error: function (jqXHR, textStatus, errorThrown) {
    console.log('Error is :' + errorThrown + '; ' + textStatus + '; ' + jqXHR.status);
    }
    });

     

    I am sure you must have gone through this informative article on working with endpoints , but just in case.

    Hope that helps.

     

    Thanks,

    Sam

    • Telstra1's avatar
      Telstra1
      Guide
      Hi Sam,

      I have added the other server to my list of Acceptable domains. and from my chrome plugin i can access the other domain and can get the JSON results back.

      but when i implement the below code in and run through Endpoint,

      $.ajax({
      type: "GET",
      url: "https://test.lithium.com/api/v1/workqueues?callback=?",
      data: {
      includeQueueBacklog: 'true',
      jsonResult: 'true'
      },
      dataType: 'json',

      success: function(jqXHR) {
      alert("Success " + jqXHR.UploadStatus);
      },
      error: function(jqXHR) {
      alert("Error " + jqXHR.status);

      }
      });

      i get the results back in my network tab, status is 200 OK. but always goes to the Error message and cant read the output json.

      error says "Call back jquery_000000<number> was not called".

      any help much appreciated.

      thanks,
      Shalini.