简体   繁体   English

等同于cURL请求的Javascript

[英]Javascript Equivalent of a cURL request

Is there a way to complete this cURL request in Javascript? 有没有办法用Javascript完成此cURL请求?

curl -X POST https://api.mongohq.com/databases/vehicles/collections/rockets/documents?_apikey=12345 \
-H "Content-Type: application/json" \
-d '{"document" : {"_id" : "bottle", "pilot_name" : "Elton John"}, "safe" : true }'

The below code is as close as I've been able to get, but it returns {"error":"Failed to create mongohq::api::document"}. 下面的代码已尽我所能,但它返回{“ error”:“无法创建mongohq :: api :: document”}。

function postsBeaconMessage (postTo, beaconMessage , targetFrame) {
    var beacon = document.createElement("form");
    beacon.method="POST";
    beacon.action = postTo;
    //beacon.target = targetFrame;

    var message = document.createElement("input") ;
    message.setAttribute("type", "hidden") ;
    message.setAttribute("name", "document") ;
    message.setAttribute("value", beaconMessage);
    beacon.appendChild(message) ;

    beacon.submit();
}

I get a 500 error when I execute this, but the cURL works fine. 执行此操作时出现500错误,但是cURL可以正常工作。 I assume it's an issue with setting the content type. 我认为设置内容类型是一个问题。 Is there a way to set the content type in the form or post object? 有没有一种方法可以在表单或发布对象中设置内容类型? It seems mongohq requires an explicit content type declaration. 似乎mongohq需要明确的内容类型声明。

I don't have access to change the same-origin policy on the server, and I'm pretty sure I won't be able to execute PHP. 我无权更改服务器上的同源策略,而且我很确定我将无法执行PHP。

Any thoughts would be helpful. 任何想法都会有所帮助。 I'm dead in the water here. 我死在这里的水中。

From your code, it looks like you are sending a POST request to https://api.mongohq.com/databases/vehicles/collections/rockets/documents?_apikey=12345 URL with {"document" : {"_id" : "bottle", "pilot_name" : "Elton John"}, "safe" : true } data and the Content-Type header set to application/json . 从您的代码看来,您正在将POST请求发送到https://api.mongohq.com/databases/vehicles/collections/rockets/documents?_apikey=12345带有{"document" : {"_id" : "bottle", "pilot_name" : "Elton John"}, "safe" : true } URL {"document" : {"_id" : "bottle", "pilot_name" : "Elton John"}, "safe" : true }数据,并将Content-Type标头设置为application/json

This can be achieved in JavaScript by doing the following 可以通过执行以下操作在JavaScript中实现

 let xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { // Check if request is completed if (xhr.readyState == XMLHttpRequest.DONE) { // Do what needs to be done here console.log(xhr.response); } } // Set the request URL and request method xhr.open("POST", "https://api.mongohq.com/databases/vehicles/collections/rockets/documents?_apikey=12345"); // Set the `Content-Type` Request header xhr.setRequestHeader("Content-Type", "application/json"); // Send the requst with Data xhr.send('{"document" : {"_id" : "bottle", "pilot_name" : "Elton John"}, "safe" : true }'); 

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

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