3

I have this JavaScript function which aims to insert a keyword in a named graph which belongs to the project Dataset.

function insert(keyword) {
    var query = "INSERT DATA {GRAPH <http://test1> {<subj> <pred>'" + keyword + "'. }}";
    var endpoint = "http://localhost:3030/project/update";
    sparqlQueryJson(query, endpoint, showResults, true);
}

I have executed Jena Fuseki with the --update option. The sparqlQueryJson function is as follows:

function sparqlQueryJson(queryStr, endpoint, callback, isDebug) {
    var querypart = "query=" + escape(queryStr);

    // Get our HTTP request object.
    var xmlhttp = null;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        // Code for older versions of IE, like IE6 and before.
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        alert('Perhaps your browser does not support XMLHttpRequests?');
    }

    // Set up a POST with JSON result format.
    xmlhttp.open('POST', endpoint, true); // GET can have caching probs, so POST
    xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xmlhttp.setRequestHeader("Accept", "application/sparql-results+json");

    // Set up callback to get the response asynchronously.
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                // Process the results
                callback(xmlhttp.responseText);

            } else {
                // Some kind of error occurred.
                alert("Sparql query error: " + xmlhttp.status + " " + xmlhttp.responseText);
            }
        }
    };
    xmlhttp.send(querypart);
};

The showResults function is, in my opinion, not very important here, since it takes the results of the query and show them in HTML.

I followed what is discussed here and here, executing the query using the http://localhost:3030/project/update. The thing is that if I execute the same query on top of the local Fuseki server with the same endpoint url by using the web, it works, but from the JavaScript code, it raises the error: "SPARQL query error: 400 Error 400: SPARQL Update: No 'update=' parameter". I'm using Ubuntu 16.04 and Jena Fuseki - version 2.4.1.

Community
  • 1
  • 1
Caleb
  • 179
  • 2
  • 17

1 Answers1

2

To solve this problem the =query parameter has to be changed to =update. In addition, a parameter with the type of the query has to be handled, i.e., update or query.

if(type==="update"){
        var querypart = "update=" + escape(queryStr);
    }else if(type === "query"){
        var querypart = "query=" + escape(queryStr);
    }
...
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    if(type==="query"){
        xmlhttp.setRequestHeader("Accept", "application/sparql-results+json");
    }
Caleb
  • 179
  • 2
  • 17