Guidance needed to frame the JQUERY code

Hello all I wanted to pass the data from text box to REST api as POST method and receive and show JSON data .Kindly guide me how to frame the JQUERY to pass the data to a REST api URL

for HTML

<textarea rows="10" cols="50">This is a Sample text.Delete to type new </textarea>

<button>Process Data</button>

Javascript

(function ($) {
    $('button').on('click', function () {
        // remove resultset if this has already been run
        $('.content ul').remove();
        // add spinner to indicate something is happening
        $('<i class="fa fa-refresh fa-spin"/>').appendTo('body');

Does the REST API support CORS?

Yes it does support IO can see it through rest api test service online but don’t know how to integrate in website

If the REST API supports CORS, then you can directly fire your AJAX requests against the API.

Hey there!
You can use $.post to do a post request. See https://api.jquery.com/jquery.post/
E.g.:

$.post( "ajax/test.html", function( data ) {
  // remove spinner
  // append data to DOM
});

Thanks can you please guide me with more code that will be needed to display the results fetched from web page on the website after the ajax POST method sends data to URL

Hey there.

You need to place the post request inside the button event listener (on click)
Also, you do not need to remove the UL, if you overwrite the innerHTML of it ($(element).html()) See example in codepen below.

How does the data look like? Will you get a JSON object or an array? What content is in there that you want to display as a list? Is it just an array of strings?

This is something you have to figure out. You could use postman to test your REST API. to see what the data looks like. If the content type of the response is JSON, it will be turned into a javascript consumable object for you. (the data parameter of the function)

I prepared a little codepen for you with a REST API I created a while ago with test data:

Pay close attention to the request in your dev tools. If you make a post or get request, observe it with the network tab (chrome or firefox) and switch to XHR: (the tab has to be loaded before the request is made)

There you can inspect the data you will receive. Alternatively, you can throw in a console.log statement in the function to see what the data looks like.

Thanks for the mail actually I want to send data through text area to GATE cloud for sentiment analysis.I am new to this and needed guidance.I Have key ID and password.The endpoint is https://cloud-api.gate.ac.uk/process-document/generic-opinion-mining-english,Can u please guide how to format the URL and which other contents needs to passed.The reference URL of the site is https://cloud.gate.ac.uk/info/help/online-api.html

This is all described in the documentation for the Gate Cloud API. If you don’t want to do it yourself, you should pay someone to do it for you, since all public APIs are intended for the programmer to read its docs before using it.

They don’t have a great documentation for javascript use cases, as it looks like :\ it should be possible though. I can understand that this is tough to understand as newcomer.

That’s why I am in forum being a developer and love to handle things myself.But I am not clear how to pass the data in URL ,Like I guess the URL structure is https://…/endpoint?key=value&key=value so should I pass key ID as in my account api details and later password is mentioned,these are the queries I wanted to know as I am interested to do things by myself.Hope I am clear

Well, this is described in the jQuery docs: http://api.jquery.com/jquery.ajax/

However, it is not described how to pass the API key required for the cloud gate API.

I just registered an account and tested around. It looks like there is actually no API key required.

Using Postman to test the endpoint worked flawlessly.

In jquery, this can be performed like this:

$.ajax({
url: 'https://cloud-api.gate.ac.uk/process-document/generic-opinion-mining-english',
type: 'POST',
data: 'some test strings I want to compare that I would hate to be mined by your api.',
contentType: "text/plain"
}, function(data) {console.log(data)})

What you do with the data and how you display it, is the only thing left to do.

Thanks it is fine I wanted to append the data from the textarea box from user to be passed as string

Thanks it is fine I wanted guidance on code to let the data from the text area to be passed to the REST API using post method like the code contains data :‘some test strings I want to compare that I would hate to be mined by your api.’,

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.