Merge pull request #440 from RockLi/improve-send-ajax-function
add new parameter settings to sendAJAX function
Showing
2 changed files
with
11 additions
and
8 deletions
... | @@ -315,7 +315,7 @@ Removes all DOM elements matching a given :ref:`XPath expression <selectors>`. | ... | @@ -315,7 +315,7 @@ Removes all DOM elements matching a given :ref:`XPath expression <selectors>`. |
315 | ``sendAJAX()`` | 315 | ``sendAJAX()`` |
316 | ----------------------------------------------------------------------------- | 316 | ----------------------------------------------------------------------------- |
317 | 317 | ||
318 | **Signature:** ``sendAJAX(String url[, String method, Object data, Boolean async])`` | 318 | **Signature:** ``sendAJAX(String url[, String method, Object data, Boolean async, Object settings])`` |
319 | 319 | ||
320 | .. versionadded:: 1.0 | 320 | .. versionadded:: 1.0 |
321 | 321 | ||
... | @@ -325,6 +325,7 @@ Sends an AJAX request, using the following parameters: | ... | @@ -325,6 +325,7 @@ Sends an AJAX request, using the following parameters: |
325 | - ``method``: The HTTP method (default: ``GET``). | 325 | - ``method``: The HTTP method (default: ``GET``). |
326 | - ``data``: Request parameters (default: ``null``). | 326 | - ``data``: Request parameters (default: ``null``). |
327 | - ``async``: Flag for an asynchroneous request? (default: ``false``) | 327 | - ``async``: Flag for an asynchroneous request? (default: ``false``) |
328 | - ``settings``: Other settings when perform the AJAX request (default: ``null``) | ||
328 | 329 | ||
329 | .. warning:: | 330 | .. warning:: |
330 | 331 | ... | ... |
... | @@ -628,17 +628,19 @@ | ... | @@ -628,17 +628,19 @@ |
628 | /** | 628 | /** |
629 | * Performs an AJAX request. | 629 | * Performs an AJAX request. |
630 | * | 630 | * |
631 | * @param String url Url. | 631 | * @param String url Url. |
632 | * @param String method HTTP method (default: GET). | 632 | * @param String method HTTP method (default: GET). |
633 | * @param Object data Request parameters. | 633 | * @param Object data Request parameters. |
634 | * @param Boolean async Asynchroneous request? (default: false) | 634 | * @param Boolean async Asynchroneous request? (default: false) |
635 | * @return String Response text. | 635 | * @param Object settings Other settings when perform the ajax request |
636 | * @return String Response text. | ||
636 | */ | 637 | */ |
637 | this.sendAJAX = function sendAJAX(url, method, data, async) { | 638 | this.sendAJAX = function sendAJAX(url, method, data, async, settings) { |
638 | var xhr = new XMLHttpRequest(), | 639 | var xhr = new XMLHttpRequest(), |
639 | dataString = "", | 640 | dataString = "", |
640 | dataList = []; | 641 | dataList = []; |
641 | method = method && method.toUpperCase() || "GET"; | 642 | method = method && method.toUpperCase() || "GET"; |
643 | var contentType = settings && settings.contentType || "application/x-www-form-urlencoded"; | ||
642 | xhr.open(method, url, !!async); | 644 | xhr.open(method, url, !!async); |
643 | this.log("sendAJAX(): Using HTTP method: '" + method + "'", "debug"); | 645 | this.log("sendAJAX(): Using HTTP method: '" + method + "'", "debug"); |
644 | xhr.overrideMimeType("text/plain; charset=x-user-defined"); | 646 | xhr.overrideMimeType("text/plain; charset=x-user-defined"); |
... | @@ -652,7 +654,7 @@ | ... | @@ -652,7 +654,7 @@ |
652 | } else if (typeof data === "string") { | 654 | } else if (typeof data === "string") { |
653 | dataString = data; | 655 | dataString = data; |
654 | } | 656 | } |
655 | xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); | 657 | xhr.setRequestHeader("Content-Type", contentType); |
656 | } | 658 | } |
657 | xhr.send(method === "POST" ? dataString : null); | 659 | xhr.send(method === "POST" ? dataString : null); |
658 | return xhr.responseText; | 660 | return xhr.responseText; | ... | ... |
-
Please register or sign in to post a comment