简体   繁体   English

Javascript与PHP动态通信

[英]Javascript Dynamically Communicate with PHP

I'm creating a system where a Javascript script extracts data from a Sage extract, and stores it in a Javascript object (JSON I guess). 我正在创建一个系统,其中Javascript脚本从Sage提取中提取数据,并将其存储在Javascript对象中(我猜JSON)。 I need to then upload the data to an SQL database via PHP. 然后我需要通过PHP将数据上传到SQL数据库。

I had thought of using an Iframe, by changing the src to the PHP pages URL, then pass GET variables to the page via the url. 我曾想过使用iframe,通过将src更改为PHP页面URL,然后通过url将GET变量传递给页面。 I was wondering if I could actually use tags to do this too? 我想知道我是否真的可以使用标签来做到这一点? By creating new images and setting the src to the PHP pages URL (again, passing GET variables to it), then the PHP page could do the rest? 通过创建新图像并将src设置为PHP页面URL(再次,将GET变量传递给它),那么PHP页面可以完成其余的工作吗? I know the image wouldn't display anything, it doesn't need to. 我知道图像不会显示任何内容,也不需要。 I just need a way to pass data to the PHP page. 我只需要一种方法将数据传递给PHP页面。

Best practices? 最佳做法?

The modern way of using JavaScript to communicate with a server is XMLHttpRequest . 使用JavaScript与服务器通信的现代方法是XMLHttpRequest By default it is asynchronous and does give you the option to change this, though synchronous requests may be considered bad practice. 默认情况下,它是异步的,并且确实为您提供了更改此选项的选项,但同步请求可能被视为不良做法。

Here is a basic example 这是一个基本的例子

function sendObject(object, uri, callback) {
    var xhr = new XMLHttpRequest(),
        data = new FormData();
    data.append('object', JSON.stringify(object));
    if (callback) xhr.addEventListener('load', callback);
    xhr.open('POST', uri);
    xhr.send(data);
}
// ex. usage
sendObject(
    {foo: "bar"},
    "/somepage.php",
    function () {console.log('completed with code:', this.status)}
);

Using a FormData saves you a little time, too. 使用FormData也可以节省一些时间。 If you can't expect it to be available, simply do 如果你不能指望它可用,那就干脆做吧

postData = encodeURIComponent(key) + '=' + encodeURIComponent(value) + '&' + etc;

As the two other answer have said, for an HTML page with Javascript to communicate with the server, a PHP page, you would need to use XMLHttpRequest , aka AJAX. 正如另外两个答案所说,对于一个带有Javascript与服务器,PHP页面进行通信的HTML页面,您需要使用XMLHttpRequest ,即AJAX。 Paul S.'s answer is the best answer with respect to how to directly use XMLHttpRequest with Javascript. Paul S.的答案是关于如何使用Javascript直接使用XMLHttpRequest的最佳答案。

However, one thing to keep in mind is that if you have to support older browsers, especially Internet Explorer version 9 or below, you'll run into quirks and it's advised to use a library for this. 但是,要记住的一件事是,如果你必须支持旧浏览器,特别是Internet Explorer 9或更低版本,你会遇到怪癖,并建议使用一个库。 For the all purpose library, which includes not only AJAX methods but also form data handling and manipulating the DOM before, during, and after your request, your best bet is to use jQuery . 对于通用库,它不仅包括AJAX方法,还包括在请求之前,期间和之后对DOM进行数据处理和操作,最好的办法是使用jQuery For example, for an AJAX request to send data from a form: 例如,对于从表单发送数据的AJAX请求:

$.ajax({
  url: 'http://www.example.com/data.php',
  data: $(form).serialize(),
  dataType: 'JSON', // JSON will be returned if possible
  type: 'POST'
}).then(function(data) { 
  ...
});

jQuery is great, but it is also a big library and if you only really want or need AJAX requests, it's better to find a smaller library or use a function that's known to work cross browser . jQuery很棒,但它也是一个很大的库,如果你真的只想要或需要AJAX请求,最好找一个更小的库或使用一个已知可以跨浏览器工作的函数 It's also important to note that jQuery has strange handling of promises, which is the way a function would say it will return a value but not right away. 同样重要的是要注意jQuery对promises的处理方式很奇怪,这就是函数会说它会返回一个值而不是立即返回值的方式。 These promises are necessary if you chain AJAX functions together without making your code contain many nested functions. 如果将AJAX函数链接在一起而不使代码包含许多嵌套函数,则这些承诺是必需的。 Two of the most well known promise libraries are rsvp.js and q . 两个最着名的promise库是rsvp.jsq

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

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