简体   繁体   English

Ajax消息最佳实践

[英]Ajax message best practices

Say I need to use ajax to asynchronously ask the server for an xml file containing relevant data. 说我需要使用ajax异步地向服务器请求包含相关数据的xml文件。 What is the best practice on what this message should look like? 此消息的外观最佳做法是什么? Should it be a string like get_data or something similar? 应该是像get_data类的字符串还是类似的东西? Should it be xml? 应该是xml吗? I don't really need long polling since its a one-time (or close to it) request. 我真的不需要长时间的轮询,因为它是一次(或接近它)的请求。

Thanks. 谢谢。

You can use standard a HTTP Post or Get to send the request to your server. 您可以使用标准的HTTP Post或Get将请求发送到您的服务器。 If you don't need to specify any parameters to your server side script ( user_id, etc ) then simply appending get_data as a url parameter will work fine. 如果您不需要在服务器端脚本中指定任何参数(user_id等),则只需将get_data作为url参数附加就可以了。

http://www.domain.com/script?get_data

If you need to send any parameters to the server in order to retrieve data it is best to encode the parameters in JSON or XML and send them as the data portion of your AJAX request. 如果需要将任何参数发送到服务器以检索数据,则最好将参数编码为JSON或XML并将其作为AJAX请求的数据部分发送。 With JQuery and JSON data: 使用JQuery和JSON数据:

$.ajax({
    type: "GET",
    url: "http://www.domain.com/script",
    data: { key: "value", key2: "value2" },
    async: true,
    dataType: "json",
    success: function( data, textStatus ) { 
        someCallbackFucntion( data );
    }   
});

The message should be the url. 该消息应为网址。

For example: http://www.example.com/get_data could return the data you need in the format you need (xml, json). 例如: http : //www.example.com/get_data可以以所需的格式(xml,json)返回所需的数据。

If you need some other data, use an other url. 如果您需要其他数据,请使用其他网址。 http://www.example.com/someotherdata http://www.example.com/someotherdata

It really depends on the purpose, if everything else is XML, go for XML. 这实际上取决于目的,如果其他所有内容都是XML,则选择XML。 Personally I perfer JSON (for the client-side at least). 我个人认为JSON(至少对于客户端而言)。

In a recent implementation I made, I used a simple POST request where the key represented the type of data and the value contained the time-interval it should return. 在最近的实现中,我使用了一个简单的POST请求,其中的键代表数据的类型,值包含应返回的时间间隔。

Which could be (jQuery): 可能是(jQuery):

$.ajax({ 
    type: "POST", 
    url: "http://www.domain.com/script", 
    data: { stock_value: "last_30_min", group_activity: "last_20" }, 
    async: true, 
    dataType: "json", 
    success: function( data, textStatus ) {  
        someCallbackFucntion( data ); 
    }    
});

A server-side controller would then handle the request appropriately and the client-side would know what data to expect when it returned. 然后,服务器端控制器将适当地处理该请求,而客户端端将知道在返回时期望什么数据。 Also the key and value would be humanly readable from both client- and server-side. 而且,密钥和值在客户端和服务器端都是人类可读的。 Of course the time-intervals could be a timestamp or otherwise, whatever fits the need. 当然,时间间隔可以是时间戳,也可以是其他时间戳,无论需要如何。

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

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