Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

If you don't use jQuery and want a nice short XMLHttpRequest 2 which works in the modern browsers and also in the mobile browsers, I suggest to useusing it this way:

Now some people will probably say that it's better to use onreadystatechange or the even the XMLHttpRequest variable name. That's wrong.

As mentioned in the comment, the use of error && synchronous does completely break the point of the answer. Which is a nice short way to use Ajax in the proper wayproperly?

But to really get out an error, the only way is to write a wrong URL in which case every browser throws an error.

Error handlers aremay maybe useful if you set custom headers, set the responseType to blob array buffer, or whatever...

In the first case, the error is inside the displayAjax() under this.statusText as Method not Allowed.

In the second case, it simply works. You have to check aton the server side if you passed the right post data.

Why add an error handler if you totally don't have any control over errors? Most of the errors are returned inside this in the callback function displayAjax().

But in these cases you should create a function only for that purpose... sometimes you need a blob, or array buffers, you can set headers, override mimetype and there is a lot more...

If you don't use jQuery and want a nice short XMLHttpRequest 2 which works in the modern browsers and also in the mobile browsers, I suggest to use it this way:

Now some people will probably say that it's better to use onreadystatechange or the even the XMLHttpRequest variable name. That's wrong.

As mentioned in the comment, the use of error && synchronous does completely break the point of the answer. Which is a nice short way to use Ajax in the proper way?

But to really get out an error, the only way is to write a wrong URL in which case every browser throws an error.

Error handlers are maybe useful if you set custom headers, set the responseType to blob array buffer, or whatever...

In the first case the error is inside the displayAjax() under this.statusText as Method not Allowed.

In the second case, it simply works. You have to check at the server side if you passed the right post data.

Why add an error handler if you totally don't have any control over errors? Most of the errors are returned inside this in the callback function displayAjax().

But in these cases you should create a function only for that purpose... sometimes you need a blob, array buffers, you can set headers, override mimetype and there is a lot more...

If you don't use jQuery and want a nice short XMLHttpRequest 2 which works in modern browsers and also in mobile browsers, I suggest using it this way:

Now some people will probably say that it's better to use onreadystatechange or even the XMLHttpRequest variable name. That's wrong.

As mentioned in the comment, the use of error && synchronous does completely break the point of the answer. Which is a nice short way to use Ajax properly?

But to get out an error, the only way is to write a wrong URL in which case every browser throws an error.

Error handlers may maybe useful if you set custom headers, set the responseType to blob array buffer, or whatever...

In the first case, the error is inside the displayAjax() under this.statusText as Method not Allowed.

In the second case, it simply works. You have to check on the server side if you passed the right post data.

Why add an error handler if you don't have any control over errors? Most of the errors are returned inside this in the callback function displayAjax().

But in these cases you should create a function only for that purpose... sometimes you need a blob, or array buffers, you can set headers, override mimetype and there is a lot more...

Active reading [<https://en.wikipedia.org/wiki/POST_%28HTTP%29> <https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods>].
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 110
  • 134

XMLHttpRequest 2 (first of all, read the answers from Benjamin Gruenbaum &and Felix Kling)

If you don't use jQuery and want a nice short XMLHttpRequest 2 which works onin the modern browsers and also onin the mobile browsers, I suggest to use it this way:

  1. It's shorter than all other functions Listed.
  2. The callback is set directly (so no extra unnecessary closures).
  3. It uses the new onload (so you don't have to check for readystate && status)
  4. There are some other situations, which I don't remember, that make the XMLHttpRequest 1 annoying.

Check out XMLHttpRequest advanced features .

It supported all *modern browsers. And I can confirm as I'mI have been using this approach since XMLHttpRequest 2 existswas created. I never had any type of problem on allin any browsers I useused.

Using the XMLHttpRequest variable name is another big error as you need to execute the callback inside the onload/oreadystatechange closures, or else you lost it.

Now if you want something more complex using postPOST and FormData you can easily extend this function:

Again ... it's a very short function, but it does get & postGET and POST.

x(url, callback); // By default it's getGET so no need to set
x(url, callback, 'post', {'key': 'val'}); // No need to set postPOST data

As you can see, I didn't implement sync... it's a bad thing.

Having said that ... why don't we do it the easy way?

As mentioned in the comment, the use of error && synchronous does completely break the point of the answer. Which is a nice short way to use Ajax in the proper way?

In the above script, you have an error handler which is statically defined, so it does not compromise the function. The error handler can be used for other functions too.

But to really get out an error, the only way is to write a wrong URL in which case every browser throws an error.

Error handlers are maybe useful if you set custom headers, set the responseType to blob array buffer, or whatever...

crossCross-domain not allowed throws an error automatically.

In the error response, there are noaren't any error codes.

There is only the this.type which is set to errorerror.

Why add an error handler if you totally don't have noany control over errors? Most of the errors are returned inside this in the callback function displayAjax().

So: NoThere isn't any need for error checks if you're able to copy and paste the URL properly. ;)

If you want to EXTENDextend the function...

I made a download manager (using ranges on both sides with resume, filereader, and filesystem), various image resizers converters using canvas, populate web SQL databases with base64images and much more...

But in these cases you should create a function only for that purpose... sometimes you need a blob, array buffers, you can set headers, override mimetype and there is a lot more...

XMLHttpRequest 2 (first of all read the answers from Benjamin Gruenbaum & Felix Kling)

If you don't use jQuery and want a nice short XMLHttpRequest 2 which works on the modern browsers and also on the mobile browsers I suggest to use it this way:

  1. It's shorter than all other functions Listed.
  2. The callback is set directly (so no extra unnecessary closures).
  3. It uses the new onload (so you don't have to check for readystate && status)
  4. There are some other situations which I don't remember that make the XMLHttpRequest 1 annoying.

Check out XMLHttpRequest advanced features

It supported all *modern browsers. And I can confirm as I'm using this approach since XMLHttpRequest 2 exists. I never had any type of problem on all browsers I use.

Using the XMLHttpRequest variable name is another big error as you need to execute the callback inside the onload/oreadystatechange closures else you lost it.

Now if you want something more complex using post and FormData you can easily extend this function:

Again ... it's a very short function, but it does get & post.

x(url, callback); // By default it's get so no need to set
x(url, callback, 'post', {'key': 'val'}); // No need to set post data

As you can see I didn't implement sync... it's a bad thing.

Having said that ... why don't do it the easy way?

As mentioned in the comment the use of error && synchronous does completely break the point of the answer. Which is a nice short way to use Ajax in the proper way?

In the above script, you have an error handler which is statically defined so it does not compromise the function. The error handler can be used for other functions too.

But to really get out an error the only way is to write a wrong URL in which case every browser throws an error.

Error handlers are maybe useful if you set custom headers, set the responseType to blob array buffer or whatever...

cross-domain not allowed throws error automatically.

In the error response, there are no error codes.

There is only the this.type which is set to error.

Why add an error handler if you totally have no control over errors? Most of the errors are returned inside this in the callback function displayAjax().

So: No need for error checks if you're able to copy and paste the URL properly. ;)

If you want to EXTEND the function...

I made a download manager (using ranges on both sides with resume, filereader, filesystem), various image resizers converters using canvas, populate web SQL databases with base64images and much more... But in these cases you should create a function only for that purpose... sometimes you need a blob, array buffers, you can set headers, override mimetype and there is a lot more...

XMLHttpRequest 2 (first of all, read the answers from Benjamin Gruenbaum and Felix Kling)

If you don't use jQuery and want a nice short XMLHttpRequest 2 which works in the modern browsers and also in the mobile browsers, I suggest to use it this way:

  1. It's shorter than all other functions Listed.
  2. The callback is set directly (so no extra unnecessary closures).
  3. It uses the new onload (so you don't have to check for readystate && status)
  4. There are some other situations, which I don't remember, that make the XMLHttpRequest 1 annoying.

Check out XMLHttpRequest advanced features .

It supported all *modern browsers. And I can confirm as I have been using this approach since XMLHttpRequest 2 was created. I never had any type of problem in any browsers I used.

Using the XMLHttpRequest variable name is another big error as you need to execute the callback inside the onload/oreadystatechange closures, or else you lost it.

Now if you want something more complex using POST and FormData you can easily extend this function:

Again ... it's a very short function, but it does GET and POST.

x(url, callback); // By default it's GET so no need to set
x(url, callback, 'post', {'key': 'val'}); // No need to set POST data

As you can see, I didn't implement sync... it's a bad thing.

Having said that ... why don't we do it the easy way?

As mentioned in the comment, the use of error && synchronous does completely break the point of the answer. Which is a nice short way to use Ajax in the proper way?

In the above script, you have an error handler which is statically defined, so it does not compromise the function. The error handler can be used for other functions too.

But to really get out an error, the only way is to write a wrong URL in which case every browser throws an error.

Error handlers are maybe useful if you set custom headers, set the responseType to blob array buffer, or whatever...

Cross-domain not allowed throws an error automatically.

In the error response, there aren't any error codes.

There is only the this.type which is set to error.

Why add an error handler if you totally don't have any control over errors? Most of the errors are returned inside this in the callback function displayAjax().

So: There isn't any need for error checks if you're able to copy and paste the URL properly. ;)

If you want to extend the function...

I made a download manager (using ranges on both sides with resume, filereader, and filesystem), various image resizers converters using canvas, populate web SQL databases with base64images and much more...

But in these cases you should create a function only for that purpose... sometimes you need a blob, array buffers, you can set headers, override mimetype and there is a lot more...

added 112 characters in body
Source Link
Quentin
  • 948.9k
  • 137
  • 1.3k
  • 1.4k

XMLHttpRequest 2 (first of all read the answers from Benjamin GruenbaumBenjamin Gruenbaum & Felix KlingFelix Kling)

XMLHttpRequest 2 (first of all read the answers from Benjamin Gruenbaum & Felix Kling)

XMLHttpRequest 2 (first of all read the answers from Benjamin Gruenbaum & Felix Kling)

corrected spelling, fixed grammer.
Source Link
Tyrone Wilson
  • 4.8k
  • 2
  • 33
  • 36
Loading
Loading
Loading
added 39 characters in body
Source Link
clearlight
  • 12.7k
  • 11
  • 61
  • 82
Loading
Copy edited (e.g. ref. <http://www.wikihow.com/Use-You%27re-and-Your>).
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 110
  • 134
Loading
Felix Kling name fixed.
Source Link
Wilfredo P
  • 4.1k
  • 30
  • 46
Loading
deleted 66 characters in body
Source Link
cocco
  • 16.8k
  • 7
  • 65
  • 77
Loading
Loading
deleted 12 characters in body
Source Link
Robert Harvey
  • 181.6k
  • 48
  • 349
  • 516
Loading
added 248 characters in body
Source Link
cocco
  • 16.8k
  • 7
  • 65
  • 77
Loading
added 248 characters in body
Source Link
cocco
  • 16.8k
  • 7
  • 65
  • 77
Loading
added 17 characters in body
Source Link
cocco
  • 16.8k
  • 7
  • 65
  • 77
Loading
added 2146 characters in body
Source Link
cocco
  • 16.8k
  • 7
  • 65
  • 77
Loading
added 2146 characters in body
Source Link
cocco
  • 16.8k
  • 7
  • 65
  • 77
Loading
added 72 characters in body
Source Link
cocco
  • 16.8k
  • 7
  • 65
  • 77
Loading
added 2 characters in body
Source Link
cocco
  • 16.8k
  • 7
  • 65
  • 77
Loading
deleted 1 characters in body
Source Link
cocco
  • 16.8k
  • 7
  • 65
  • 77
Loading
deleted 1 characters in body
Source Link
cocco
  • 16.8k
  • 7
  • 65
  • 77
Loading
deleted 1 characters in body
Source Link
cocco
  • 16.8k
  • 7
  • 65
  • 77
Loading
Source Link
cocco
  • 16.8k
  • 7
  • 65
  • 77
Loading
lang-js

AltStyle によって変換されたページ (->オリジナル) /