https://github.com/lukas-stuehrk/xmlhttprequest
An implementation of the JavaScript XMLHTTPRequest object to extend JavaScriptCore.
https://github.com/lukas-stuehrk/xmlhttprequest
ios javascriptcore objective-c xmlhttprequest
Last synced: 12 months ago
JSON representation
An implementation of the JavaScript XMLHTTPRequest object to extend JavaScriptCore.
- Host: GitHub
- URL: https://github.com/lukas-stuehrk/xmlhttprequest
- Owner: Lukas-Stuehrk
- License: mit
- Created: 2015-02-23T21:27:42.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2017-12-19T18:50:26.000Z (over 8 years ago)
- Last Synced: 2024-04-27T02:22:27.984Z (almost 2 years ago)
- Topics: ios, javascriptcore, objective-c, xmlhttprequest
- Language: Objective-C
- Size: 23.4 KB
- Stars: 66
- Watchers: 9
- Forks: 14
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# XMLHTTPRequest
In iOS 7, Apple introduced the possibility to [execute JavaScript via the JavaScriptCore JavaScript
engine] (http://nshipster.com/javascriptcore/). Unfortunately, JavaScriptCore is missing some
objects and functions a JavaScript environment of a browser would have. Especially the
`XMLHttpRequest` (see the [Mozilla documentation]
(https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) object needed for AJAX requests
is not provided by JavaScriptCore. This library implements this missing object, so it is possible to
use JavaScript libraries which were originally developed for in-browser use in your Objective-C
(or Swift) application without the need to use a hidden WebView.
## Provided functions
This library tries to implement the full XMLHttpRequest specification. Currently not all
functionality is implemented yet. The current limitations are:
* Synchronous calls are not supported.
* The `onload` and `onerror` callbacks are currently not supported.
* The `upload` callback is currently not supported.
* The `timeout` property is currently not supported.
It is planned to support all functionality of the HTML5 specification at some point.
## How to use it
Create a new instance of the `XMLHTTPRequest` class. Then call the `extend:` method and pass either
a `JSContext` instance or a `JSValue` instance. The given object will be extend with the
`XMLHTTPRequest` object.
```objc
#import
...
JSContext *jsContext = [JSContext new];
XMLHttpRequest *xmlHttpRequest = [XMLHttpRequest new];
[xmlHttpRequest extend:jsContext];
```
The JavaScript context now has a XMLHTTPRequest object you can use like the object found in
browsers. Example (JavaScript):
```JavaScript
request.open('GET', 'http://example.com');
request.setRequestHeader('Accept', 'text/html');
request.setRequestHeader('X-Foo', 'bar');
request.send();
```
### More Stuff
If you are interested in more polyfills for missing browser functionality in JavaScriptCore, there is
a window timers implementation (`setTimeout`, `setInterval`, ...) called [WindowTimers]
(https://github.com/Lukas-Stuehrk/WindowTimers).