如何从javascript发送curl请求? [英] How can I send a curl request from javascript?

查看:109
本文介绍了如何从javascript发送curl请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发送此

  curl https://fcm.googleapis.com/fcm/send \ 
-HContent-Type:application / json\
-H授权:key =< MY API KEY> \
-d'{notification:{title:Hello world,body:你有新消息,icon:icon / path,click_action: page / path},to:< DEVICE TOKEN>}'

来自一个js文件到我想要的地方。考虑到你给我的想法,现在我正在尝试这个:

  $ .ajax({
url:https://fcm.googleapis.com/fcm/send,
类型:'POST',
dataType:'json',
header:{
' Access-Control-Allow-Origin':'*',
'授权':'< APY KEY>',
'contentType':'application / json',
},
数据:{
'title':'Hello world!',
'body':'你有新消息',
'icon':'icon / path',
'click_action':'page / path',
'到':'< DEVICE TOKEN>',
},
成功:函数(结果){
alert(JSON.stringify(data));
},
错误:function(error){
alert(无法获取数据);
}
});

但我收到此错误:对预检请求的响应未通过访问控制检查:否'Access-Control-Allow-Origin'标题存在

解决方案

JavaScript中不存在Curl,而是可以使用XMLHttpRequest 。为了使它更容易,您可以使用jQuery发送AJAX请求。



以下是一些示例代码:



< pre class =snippet-code-js lang-js prettyprint-override> $ .ajax({type:POST,url:http://yourdomain.com/example.php, data:{api_key:xxxxxxxx},success:function(data){console.log(data); //在请求成功时执行某些操作},dataType:json});


I would like to send this

curl https://fcm.googleapis.com/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.com/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

解决方案

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.com/example.php",
  data: {
    api_key: "xxxxxxxx"
  },
  success: function(data) {
    console.log(data);
    //do something when request is successfull
  },
  dataType: "json"
});

这篇关于如何从javascript发送curl请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆