开发者

how to implement this data transfer in javascript? [closed]

开发者 https://www.devze.com 2023-03-05 20:49 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_开发知识库 Closed 11 years ago.

i need to send to server 50 bytes of data and receive 200 bytes in response without reloading a page. How can i do it?? What is the most crossbrowser way??


If you don't want to use a library, take a look at this page. Nice info to make your ajax call cross browser.

And, if the data is binary, encode it

This is a snippet copied verbatim from ajax for N00bs (I strongly suggest you to read not only that post, but all of them in that site):

function callback(serverData, serverStatus) {
  alert(serverData);
}
// Called automatically when we get data back from server
// Display an alert box with the recieved data
function ajaxRequest() {
  var AJAX = null;
  // Initialize the AJAX variable.
  if (window.XMLHttpRequest) {
    // Does this browser have an XMLHttpRequest object?
    AJAX=new XMLHttpRequest();
    // Yes -- initialize it.
  } else {
    // No, try to initialize it IE style
    AJAX=new ActiveXObject("Microsoft.XMLHTTP"); // Wheee, ActiveX, how do we format c: again?
  }
  // End setup Ajax.
  if (AJAX==null) {
    // If we couldn't initialize Ajax...
    alert("Your browser doesn't support AJAX."); // Sorry msg.
    return false
    // Return false, couldn't set up ajax
  }
  AJAX.onreadystatechange = function() {
    // When the browser has the request info..
    if (AJAX.readyState==4 || AJAX.readyState=="complete") { // see if the complete flag is set.
      callback(AJAX.responseText, AJAX.status);
      // Pass the response to our processing function
    }
    // End Ajax readystate check.
  }
  var url='http://somedomain.com/getdata.php?doc=sometext.txt'; // This is the URL we will call.
  AJAX.open("GET", url, true);
  // Open the url this object was set-up with.
  AJAX.send(null);
  // Send the request.
}


The easiest way is to use something like jQuery: http://api.jquery.com/jQuery.get/

You could setup the AJAX request yourself if you need the code to be really small. Although most users have jQuery cached (use the Google source) so there should be minimal load impact.

0

精彩评论

暂无评论...
验证码 换一张
取 消