Support Questions

Find answers, ask questions, and share your expertise

apache livy and ajax post requests: Method Not Allowed

avatar

Good morning everyone,

can someone help me to see why this jquery code can note create a session to the apache livy? I got these errors :

OPTIONS http://localhost:8998/ 405 (Method Not Allowed) and Failed to load http://localhost:8998/: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8084' is therefore not allowed access. The response had HTTP status code 405.

This is the code I used :

$.ajax({
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            url: 'http://localhost:8998',
            data: {kind: 'spark'},
            success: function (resultat) {
                document.getElementById(response).innerHTML = resultat;
                console.log(resultat);
            }
        });

Thank you for your help

1 ACCEPTED SOLUTION

avatar

@Melchicédec NDUWAYO

I tried deleting one comment and ended up deleting full thing, sorry. I have a backup of the instructions I provided initially. Later we found this was related to CORS and that Livy does not support it yet. So alternatives are using an http proxy or your web application to redirect the requests.

web page --> your web app backend --> livy

I recommend you use google Postman as it has a feature to generate jquery code.

Here is the jquery code to start a session:

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://localhost:8999/sessions",
  "method": "POST",
  "headers": {
    "Content-Type": "application/json",
    "X-Requested-By": "user",
    "Cache-Control": "no-cache",
  },
  "processData": false,
  "data": "{\"kind\": \"spark\"}"
}
 
$.ajax(settings).done(function (response) {
  console.log(response);
});

Of course on the done function instead of printing to console you need to parse the json and grab the session id to use later as a variable. Then you will need to wait until session state transitions to idle, in oder to do this you need to execute the following request in regular intervals until state = idle:

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://c24-node2:8999/sessions/<session_id>",
  "method": "GET",
  "headers": {
    "Cache-Control": "no-cache",
    "Postman-Token": "60246911-890b-4f81-8635-da52e54ef633"
  }
}
 
$.ajax(settings).done(function (response) {
  console.log(response);
});

Above done function needs to parse the json and check if state is idle Once state is idle we can submit code using the following jquery code:

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://c24-node2:8999/sessions/<session_id>/statements",
  "method": "POST",
  "headers": {
    "Content-Type": "application/json",
    "X-Requested-By": "user",
    "Cache-Control": "no-cache",
    "Postman-Token": "aece2722-cbdd-48a2-ae9a-821aba97a2b6"
  },
  "processData": false,
  "data": "{\"code\": \"1 + 1\"}"
}
 
$.ajax(settings).done(function (response) {
  console.log(response);
});

The above is pushing code "1+1" - Same as before with session we need to grab the result in done function and save the statement id to use later to retrieve the results. With session id and statement id we can retrieve the results of the execution by running the following jquery:

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://c24-node2:8999/sessions/<session_id>/statements/<statment_id>",
  "method": "GET",
  "headers": {
    "Content-Type": "application/json",
    "X-Requested-By": "user",
    "Cache-Control": "no-cache",
    "Postman-Token": "3be8a12e-af07-4567-83ff-e37b48974e76"
  }
}
 
$.ajax(settings).done(function (response) {
  console.log(response);
});

The result of the above execution should be similar to this:

{
    "id": 1,
    "code": "1 + 1",
    "state": "available",
    "output": {
        "status": "ok",
        "execution_count": 1,
        "data": {
            "text/plain": "res1: Int = 2"
        }
    },
    "progress": 1
}

Hopefully this will help you. Feel free to update this thread if you have any questions. HTH *** If you found this answer addressed your question, please take a moment to login and click the "accept" link on the answer.

View solution in original post

11 REPLIES 11

avatar

@Melchicédec NDUWAYO you will probably need to escape quotes in the strings (or try using single quotes instead) so that it wont break the javascript. If you agree lets discuss this in different thread as it seems to be specific to running code with strings now and the initial question has been addressed.

avatar
@Felix Albani

, so I have to create another thread for this or you you will create it?

Thank you.