Forum Discussion

SheryBulos's avatar
5 years ago

how to make implement endpoint with ajax?

Hello everyone,

My question is how can I implement endpoint  to achieve this?

specifically I want to make endpoint that
  1. take as a parameter file name
  2. get the file from the assets by name
  3. parse it and return the file content

I want to call this end point from custom component. here is what I tried it however I don't know how should I return the data on success to the component? 



 

<@liaAddScript>
	;(function ($) {
		$.ajax({
           url : '${asset.get('/html/assets/filename')}',
           type: 'GET',                    
           success : function(data){
           		console.log(data);
           }, 
	       error : function(data){
	        
	       }

        });
	})(LITHIUM.jQuery); 
</@liaAddScript>

 

 

 

 

Excuse me, if the question sound intuitive,  I am a bit new to Khoros.

Thanks in advance,
Shery

  • peterlu's avatar
    peterlu
    5 years ago

    SheryBulos The only way to do server side parsing is via Endpoint. Endpoint supports Http Client API (see Khoros doc for HTTP Client https://developer.khoros.com/khoroscommunitydevdocs/reference/httpclientrequest). So you put the JSON file in /html/assets or AWS S3 bucket. Then use HTTP client api to fetch the json. The JSON object cannot be too complicated. I think you only supports plain simple JSON object. You can have a try.

    Then you will have to use frontend javascript AJAX to talk to the endpoint. So the endpoint behaves like a proxy.

    Another way to do it, is to put the JSON in AWS S3 bucket and open the CORS policy. So your frontend AjAX can pull JSON from AWS S3 directly. This way, the JSON object can have any structure.

  • SheryBulos it depends on what type of the file you are dealing with. If it is a JSON file, then it is easier. Then you can fetch the JSON data, and print it out to the component DOM.

    Also this ${asset.get('/html/assets/filename')} may not work with AJAX. Since asset.get is gonna print out the CDN domain, your AJAX call may have CORS issue which will fail the AJAX.

    • SheryBulos's avatar
      SheryBulos
      Ace

      peterlu  first of all, Thank you so much for your help.

      just to make sure that we are on the same page, this file is json file so how to parse it and pass the content to macro ?

      • peterlu's avatar
        peterlu
        Champion

        SheryBulos The only way to do server side parsing is via Endpoint. Endpoint supports Http Client API (see Khoros doc for HTTP Client https://developer.khoros.com/khoroscommunitydevdocs/reference/httpclientrequest). So you put the JSON file in /html/assets or AWS S3 bucket. Then use HTTP client api to fetch the json. The JSON object cannot be too complicated. I think you only supports plain simple JSON object. You can have a try.

        Then you will have to use frontend javascript AJAX to talk to the endpoint. So the endpoint behaves like a proxy.

        Another way to do it, is to put the JSON in AWS S3 bucket and open the CORS policy. So your frontend AjAX can pull JSON from AWS S3 directly. This way, the JSON object can have any structure.