简体   繁体   中英

javascript alert not working when API http request has json error (nothing found)

I'm using a thesaurus API,

http://thesaurus.altervista.org

to get synonyms to print to the page. In this demo version,

http://plnkr.co/edit/6pNYlXcgiwxHz00AY5CT?p=preview

it works when you type 'peace', otherwise, in console, if we follow the link, we see a GET error:

{"error":"the key test_only is restricted, please use your own key!"}

(EDIT: to clarify: Demo mode is not the issue. I want to make an alert.) For errors, I want the browser to alert "Oops, word not found. Please choose another word."

Here is the js code from the demo:

//function activated on user input
    function myFunc(){
        var userInput = document.getElementById('userInput');
        var result = userInput.value; 
        var s = document.createElement("script");

        //This demo works with the keyword 'peace' only. So searching for e.g. 'hat' will cause an error.
        s.src = "http://thesaurus.altervista.org/service.php?word=" + result + "&language=en_US&output=json&key=test_only&callback=process"; // NOTE: replace test_only with your own KEY 
        document.getElementsByTagName("head")[0].appendChild(s); 

        window.process = function (result) {
            //my attempt at throwing an error
            if(result.error){alert("Oops, word not found. Please choose another word.");}
            //normal processing..
            output = ""; 
            for (key in result.response) { 
            list = result.response[key].list; 
            output += list.synonyms+"<br>"; 
        } 
            if (output) 
            document.getElementById("synonyms").innerHTML = output; 
        }
    }

I did some research and found this answer (it's jQuery but mine's not but it might be relevant?):

https://stackoverflow.com/a/2568788

Any help much appreciated, thanks.

It's because you need to generate your own key. The key you are using is just a sample key.

Right there .

Then you'll change test_only is the URL by the generated key.

Anyhow, that perhaps means that the service is not free of use, so be careful and read the license if you plan to use it commercially.

You need to replace the test API key with your API key.

You can get your API key by going to http://thesaurus.altervista.org/mykey and signing up or logging in.

You even have a comment in your code telling you to do this:

 s.src = "http://thesaurus.altervista.org/service.php?word=" + result + "&language=en_US&output=json&key=test_only&callback=process"; 
// NOTE: replace test_only with your own KEY

So if your API key 123456789, your URL will look like this: s.src = "http://thesaurus.altervista.org/service.php?word=" + result + "&language=en_US&output=json&key=123456789&callback=process";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM