Search code examples
javascriptjqueryajaxweb-servicesmashape

Error while trying to consume a Mashape webservices using javascript



I'm trying to consume a mashape api using ajax, javascript.

I have a text area and a button in my html :

<div>
    <textarea id="message" placeholder="Message">
    </textarea>

    <br><br>

    <button id="mb" onclick="success();">Analyse</button>
</div>

And a really simple javascript success() function :

function service(){
    $.ajax({
        url: 'https://loudelement-free-natural-language-processing-service.p.mashape.com/nlp-text/', // The URL to the API. You can get this by clicking on "Show CURL example" from an API profile
        type: 'GET', // The HTTP Method
        data: {text: $("#m").val()}, // Parameters go here
        datatype: 'json',
        success: function (data) {
            alert(data);
        },
        error: function (err) {
            alert(err);
        },
        beforeSend: function (xhr) {
            xhr.setRequestHeader("X-Mashape-Authorization", "<MY MASHAPE KEY>"); // Enter your Mashape key here
        }
    });
};

however when i trigger the function, the following error is displayed in my browser console : ReferenceError: success is not defined

Any idea on how to solve this error ?


Solution

  • The function success() dosn't exist however you have a service() function either you change the invocation success() -> service() or you change the function name from function success -> function service