admin 管理员组

文章数量: 1086019

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"
});

本文标签: How can I send a curl request from javascriptStack Overflow