简体   繁体   English

如何转换我的curl方法在我的JavaScript脚本中工作?

[英]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. javascript初学者,我正在寻找将curl方法转换为可在javascript脚本中工作的方法。

Here is my curl method: 这是我的curl方法:

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

My test with ajax (doesn't work): 我对ajax的测试(无效):

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

The AJAX call is missing the accept header. AJAX调用缺少accept标头。

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. 我建议在您正在使用的jquery ajax方法上使用访存API 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! 如果您考虑使用它,我还建议您学习如何处理Promises ,以防万一!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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