如何设置简单的firebase ajax请求?

编程入门 行业动态 更新时间:2024-10-24 20:14:50
本文介绍了如何设置简单的firebase ajax请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道我可以使用set来点击Firebase,但我想使用AJAX,所以我尝试了下面的代码。当我在浏览器中加载test.html时,控制台会说 -

I know I can use set to hit Firebase, but I want to use AJAX instead so I tried the below code. When I load test.html in my browser, the console says -

XMLHttpRequest无法加载 jleiphonebook.firebaseio/json 。请求的资源上不存在Access-Control-Allow-Origin标头。因此不允许原点'null'访问。响应的HTTP状态代码为405.

XMLHttpRequest cannot load jleiphonebook.firebaseio/json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 405.

// text.html

//text.html

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Firebase Test</title> <script src='cdn.firebase/js/client/2.2.1/firebase.js'></script> </head> <body> <div id="hi"></div> <script src="code.jquery/jquery-1.12.2.min.js" integrity="sha256-lZFHibXzMHo3GGeehn1hudTAP3Sc0uKXBXAzHX1sjtk=" crossorigin="anonymous"></script> <script> $(document).ready(function () { var param = {lastName: "Doe", firstName: "John"}; $.ajax({ url: 'jleiphonebook.firebaseio/json', type: "POST", data: param, success: function () { alert("success"); } }); }); </script> </body> </html>

// firebase规则

//firebase rules

{ "rules": { ".read": true, ".write": true } }

推荐答案

Firebase希望正文是一个JSON字符串,所以你'我需要对其进行字符串化:

Firebase expects the body to be a JSON string, so you'll need to stringify it:

$(document).ready(function () { var param = {lastName: "Doe", firstName: "John"}; $.ajax({ url: 'jleiphonebook.firebaseio/.json', type: "POST", data: JSON.stringify(param), success: function () { alert("success"); }, error: function(error) { alert("error: "+error); } }); });

顺便提一下:

$.post('jleiphonebook.firebaseio/.json', JSON.stringify(param), function () { alert("success"); } );

更多推荐

如何设置简单的firebase ajax请求?

本文发布于:2023-11-24 19:51:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626588.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何设置   简单   ajax   firebase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!