简体   繁体   中英

How to to convert my curl method to work in my javascript script?

Beginner in javascript, I'm looking for ways to convert my curl method to work in my javascript script.

Here is my curl method:

curl -X GET "http://model.dbpedia-spotlight.org/en/annotate?text=beyonce" -H "accept: text/html"

My test with ajax (doesn't work):

$.ajax({
    method: "GET",
    url: "http://model.dbpedia-spotlight.org/en/annotate?text=beyonce"
})

The AJAX call is missing the accept header.

Once you add this in, it seems to work. Please see the example below:

 $.ajax({ method: "GET", url: "http://model.dbpedia-spotlight.org/en/annotate?text=beyonce", data: { confidence: "0.60" }, headers: {"accept" : "text/html"} }).done((res) => $("#res").append(res)); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="res"></div> 

I'd recommend using the fetch API over this jquery ajax method you're using. It's quite supported

fetch("http://model.dbpedia-spotlight.org/en/annotate?text=beyonce").then(response => response.text())

If you consider using it, I'd also recommend learning how to deal with Promises in case you don't know!

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