weixin_33698043 2016-08-05 22:57 采纳率: 0%
浏览 66


I'm trying to convert the curl code from an API called TextRazor to AJAX XMLHttp because of platform limitations. I have tried many solutions from similar questions by the community but can't seem to get any data back or just a "400: Bad Request". If it matters, from the documentation calling the API looks like this:

curl -X POST \
-H "x-textrazor-key: YOUR_API_KEY" \
-d "extractors=entities,entailments" \
-d "text=Spain's stricken Bankia expects to sell off..." \

My current AJAX XMLHttp code looks like this:

var xhttp = new XMLHttpRequest();
var url = "https://api.textrazor.com/";
var params = "extractors=entities&text=Spain's stricken Bankia expects to sell...";
xhttp.open("POST", url, true);

xhttp.setRequestHeader("x-textrazor-key", "YOUR_API_KEY");
xhttp.setRequestHeader("Content-length", params.length);

xhttp.onreadystatechange = function() {
if(xhttp.readyState == 4 && xhttp.status == 200) {


Thanks for your support!

  • 写回答

1条回答 默认 最新

  • weixin_33716154 2016-08-05 23:19

    You are running into the same origin policy.

    Because you are making a cross-origin request and adding a custom header, the browser is making a preflight OPTIONS request before it makes the request you are asking for.

    The server you are making the request to is not prepared to respond to an OPTIONS request so it throws a 400 back at you.

    To fix this:

    • They should support CORS
    • They should provide an alternative means to get the data such as JSONP (not recommended, we have CORS now)
    • You should not make the request directly to their server from the browser



    • ¥20 如何用Numpy库的向量化方法提高delta对冲的计算效率?(代码已附,只需修改第三个函数,在一小时内绘制出散点图即可)
    • ¥15 yolov5双目识别输出坐标代码报错
    • ¥15 这个代码有什么语法错误
    • ¥15 给予STM32按键中断与串口通信
    • ¥15 使用QT实现can通信
    • ¥15 关于sp验证的一些东西,求告知如何解决,
    • ¥35 关于#javascript#的问题:但是我写的只能接码数字和字符,帮我写一个解码JS问题
    • ¥15 prophet运行报错,如何解决?
    • ¥15 用GPU跑pytorch搭建的LSTM的时候出现了奇怪的报错
    • ¥20 前端数据是从session等作用域拿到的,如何取值继续传递后端呢