Posting data back to server

Topics: General, Language Specification
Oct 24, 2012 at 3:29 AM

How can we post data back to the server via AJAX in TypeScript (not jQuery, or backbone or prototype)?

 

I've been searching around and read most of the spec, but I haven't been able to find anything relevant. Maybe I missed something?

Oct 24, 2012 at 2:13 PM

TypeScript is a language, not a library. That is, it only includes language-level features, but no communication primitives. 

You can use all of the mentioned libraries with TypeScript. 

Oct 24, 2012 at 2:41 PM
mihailik wrote:

TypeScript is a language, not a library. That is, it only includes language-level features, but no communication primitives. 

You can use all of the mentioned libraries with TypeScript. 


Thank you for getting back to me. So as I understand, there is no way to perform AJAX stuff with TypeScript unless you import/include a third-party library such as jQuery?

Oct 24, 2012 at 4:42 PM

TypeScript compiles into JavaScript.

Every part of valid JavaScript (ECMAScript) you write in your code will also be in the final compiled code.
You can use jQuery but it's nothing more than some useful functions wrapping native JavaScript.

Here is an example with raw JavaScript:

// Note that you can also
// use the new XMLHttpRequest Level 2
// events
// See here
// http://www.w3.org/TR/XMLHttpRequest/

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
  if (xhr.readyState == 4) {
    if (xhr.status == 200) {
      // All right - data is stored in xhr.responseText
    }
    else {
      // Server responded with another status code!
    }
  }
}
xhr.open("GET", "yourscript.php");
xhr.send();
Oct 25, 2012 at 6:03 AM

Thank you ComFreek. That was much appreciated.