简体   繁体   English

Javascript在firefox插件中通过POST发送数据

[英]Javascript sending data via POST in firefox addon

I have a mysql database, with a php form. 我有一个带有php表单的mysql数据库。 Normally, people use the php form on my website to add to the mysql database. 通常,人们使用我的网站上的php表单添加到mysql数据库。 I have been building a firefox addon to let them use the form without visiting the site directly to add data to the mysql database. 我一直在构建一个firefox插件,让他们使用表单而不直接访问网站将数据添加到mysql数据库。 Now I am stuck... 现在我卡住了......

I have the form data I want to add to the mysql database, but how can I send it to the mysql database from the addon? 我有要添加到mysql数据库的表单数据,但是如何从插件将其发送到mysql数据库? What's the best way to do this? 最好的方法是什么? Would you send it to the php form first or is there a direct way? 你会先将它发送到php表单还是有直接的方式? Is it possible to go straight to mysql? 有可能直接进入mysql吗? The firefox addon is coded in javascript. firefox插件用javascript编码。

Thanks! 谢谢!

It sounds like Ajax would be the way to go. 这听起来像Ajax将是要走的路。 This post may be helpful to you: HTTP POST in javascript in Firefox Extension . 这篇文章可能对您有所帮助: Firefox扩展中的javascript中的HTTP POST

Jan Hančič is right : the best way is to use XMLHttpRequest. JanHančič是对的:最好的方法是使用XMLHttpRequest。

Here's an example : 这是一个例子:

var xhr = new XMLHttpRequest();
xhr.open("post", "http://ex.ample.com/file.php", true);
xhr.onreadystatechange = function() {
    if(this.readyState == 4) {
        // Do something with this.responseText
    }
}
xhr.send("var1=val1&var2=val2");

There are plenty of tutorials and references on the web about AJAX and the xhr object. 网上有很多关于AJAX和xhr对象的教程和参考资料。

Use Ajax to send data but do not use xmlHttpRequest directly in your code. 使用Ajax发送数据但不要在代码中直接使用xmlHttpRequest。

Use a popular javascript library like jquery to send data to the server. 使用像jquery这样的流行javascript库将数据发送到服务器。

Edit : Removed irrelevant parts about browser compatibility. 编辑 :删除了有关浏览器兼容性的不相关部分。

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

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