最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I send a curl request from javascript? - Stack Overflow

programmeradmin8浏览0评论

I would like to send this

curl  \
     -H "Content-Type: application/json" \
     -H "Authorization: key=<MY API KEY>" \
     -d '{ "notification": {"title": "Hello world", "body": "you got a new message", "icon": "icon/path","click_action" : "page/path"},"to" : "<DEVICE TOKEN>"}'

from a js file to wherever I want. Considering the ideas you are giving me bellow, now I'm trying with this:

        $.ajax({
            url: "",
            type: 'POST',
            dataType: 'json',
            headers: {
                'Access-Control-Allow-Origin' : '*',
                'Authorization': '<APY KEY>',
                'contentType': 'application/json',
            },
            data: {
                'title': 'Hello world!', 
                'body': 'you got a new message', 
                'icon': 'icon/path', 
                'click_action' : 'page/path', 
                'to' : '<DEVICE TOKEN>',
            },
            success: function (result) {
              alert(JSON.stringify(data));
            },
            error: function (error) {
               alert("Cannot get data");
            }
        });

but I'm getting this error: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present

I would like to send this

curl https://fcm.googleapis./fcm/send \
     -H "Content-Type: application/json" \
     -H "Authorization: key=<MY API KEY>" \
     -d '{ "notification": {"title": "Hello world", "body": "you got a new message", "icon": "icon/path","click_action" : "page/path"},"to" : "<DEVICE TOKEN>"}'

from a js file to wherever I want. Considering the ideas you are giving me bellow, now I'm trying with this:

        $.ajax({
            url: "https://fcm.googleapis./fcm/send",
            type: 'POST',
            dataType: 'json',
            headers: {
                'Access-Control-Allow-Origin' : '*',
                'Authorization': '<APY KEY>',
                'contentType': 'application/json',
            },
            data: {
                'title': 'Hello world!', 
                'body': 'you got a new message', 
                'icon': 'icon/path', 
                'click_action' : 'page/path', 
                'to' : '<DEVICE TOKEN>',
            },
            success: function (result) {
              alert(JSON.stringify(data));
            },
            error: function (error) {
               alert("Cannot get data");
            }
        });

but I'm getting this error: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present

Share Improve this question edited Mar 5, 2018 at 20:31 neomat asked Mar 5, 2018 at 18:33 neomatneomat 1031 gold badge1 silver badge6 bronze badges 3
  • developer.mozilla/en-US/docs/Web/API/Fetch_API – jmargolisvt Commented Mar 5, 2018 at 18:37
  • The error message does not mean your request, so you don't have to put these headers in your request. Instead it refers to the response from firebase, which you are trying to access. To enable CORS, you can have a look here: stackoverflow./questions/42755131/… – Martin Seeler Commented Mar 5, 2018 at 20:10
  • Possible duplicate of Executing curl from Javascript? – Mehdi Dehghani Commented Feb 26, 2019 at 11:39
Add a ment  | 

1 Answer 1

Reset to default 3

Curl doesn't exist in JavaScript, instead you can use XMLHttpRequest. To make it even more easy, you can use jQuery to send an AJAX request.

Here is some example code:

$.ajax({
  type: "POST",
  url: "http://yourdomain./example.php",
  data: {
    api_key: "xxxxxxxx"
  },
  success: function(data) {
    console.log(data);
    //do something when request is successfull
  },
  dataType: "json"
});

发布评论

评论列表(0)

  1. 暂无评论