コード例 #1
0
        public void Then(
            Delegate fulfilledHandler, Delegate errorHandler = null, Delegate progressHandler = null)
        {
            var req = new XMLHttpRequest();

            req.OnReadyStateChange = () => {
                if (req.ReadyState != AjaxReadyState.Done)
                {
                    return;
                }

                if (req.Status >= 200 && req.Status < 400)
                {
                    Logger.Debug(GetType(), "upload success");
                    fulfilledHandler?.Call(null, ResultHolder <XMLHttpRequest> .CreateSuccess(req));
                    return;
                }

                Logger.Debug(GetType(), "upload error");
                fulfilledHandler?.Call(
                    null, ResultHolder <XMLHttpRequest> .CreateFailure(req.ResponseText, null, req));
            };
            req.Open(_method, _url, true);

            req.SetRequestHeader("Cache-Control", "no-cache");
            req.SetRequestHeader("Pragma", "no-cache");

            var tzOffset = Script.Eval <string>("new Date().getTimezoneOffset() + \"\"");

            req.SetRequestHeader(Philadelphia.Common.Model.Magics.TimeZoneOffsetFieldName, tzOffset);

            try {
                var tzCode = Script.Eval <object>("Intl.DateTimeFormat().resolvedOptions().timeZone");
                if (Script.IsDefined(tzCode))
                {
                    req.SetRequestHeader(Philadelphia.Common.Model.Magics.TimeZoneCodeFieldName, tzCode.ToString());
                }
            } catch (Exception) {
                //most likely it is unsupported
                Logger.Error(GetType(), "could not determine timeZone");
            }

            if (CsrfToken != null)
            {
                req.SetRequestHeader(Philadelphia.Common.Model.Magics.CsrfTokenFieldName, CsrfToken);
            }

            if (_frmData != null)
            {
                req.Send(_frmData);
            }
            else
            {
                req.Send(_dataToPost);
            }
        }
コード例 #2
0
        public static async Task <string> GetAsync(string url)
        {
            var tcs     = new TaskCompletionSource <string>();
            var xmlHttp = new XMLHttpRequest();

            xmlHttp.Open("GET", url, true);
            xmlHttp.SetRequestHeader("Content-Type", "application/json");
            xmlHttp.OnReadyStateChange = () =>
            {
                if (xmlHttp.ReadyState == AjaxReadyState.Done)
                {
                    if (xmlHttp.Status == 200 || xmlHttp.Status == 304)
                    {
                        tcs.SetResult(xmlHttp.ResponseText);
                    }
                    else
                    {
                        tcs.SetException(new Exception(xmlHttp.ResponseText));
                    }
                }
            };

            xmlHttp.Send();
            return(await tcs.Task);
        }
コード例 #3
0
        public static void addArduinoLightBox(HTMLBoxStructure parent, string text, string url, string json = null)
        {
            HTMLBox box = new HTMLBox(text);

            box.Action(delegate {
                Action <Event> actionError = delegate(Event e) {
                    Bridge.Console.Error("Error", e);
                    box.Color(error, 1000);
                };
                Action <Event> actionSucces = delegate(Event e) {
                    Bridge.Console.Info("Succes", e);
                    box.Color(succes, 1000);
                    Vibrate(250);
                };
                XMLHttpRequest request = new XMLHttpRequest();
                if (json != null)
                {
                    request.Open("POST", url, true);
                    request.SetRequestHeader("Content-Type", "application/json");
                    request.Timeout = 1250;
                    request.Send(json);
                }
                else
                {
                    request.Open("GET", url, true);
                    request.Timeout = 1250;
                    request.Send();
                }
                request.OnTimeout += actionError;
                request.OnAbort   += actionError;
                request.OnError   += actionError;
                request.OnLoad    += (Event e) => {
                    if (request.ResponseText.Contains("Geaccepteerd"))
                    {
                        actionSucces(e);
                    }
                    else
                    {
                        actionError(e);
                    }
                };
            });
            parent.addBox(box);
        }
コード例 #4
0
        static async Task <object> PostJsonAsync(string url, Type returnType, object[] parameters)
        {
            var tcs     = new TaskCompletionSource <object>();
            var xmlHttp = new XMLHttpRequest();

            xmlHttp.Open("POST", url, true);
            xmlHttp.SetRequestHeader("Content-Type", "application/json");
            xmlHttp.OnReadyStateChange = () =>
            {
                if (xmlHttp.ReadyState == AjaxReadyState.Done)
                {
                    if (xmlHttp.Status == 200 || xmlHttp.Status == 304)
                    {
                        var json = JSON.Parse(xmlHttp.ResponseText);

                        if (json == null)
                        {
                            tcs.SetResult(null);
                        }
                        else if (Script.IsDefined(json["$exception"]) && json["$exception"].As <bool>())
                        {
                            tcs.SetException(new Exception(json["$exceptionMessage"].As <string>()));
                        }
                        else
                        {
                            var result = Json.Deserialize(xmlHttp.ResponseText, returnType);
                            tcs.SetResult(result);
                        }
                    }
                    else
                    {
                        tcs.SetException(new Exception(xmlHttp.ResponseText));
                    }
                }
            };

            var serialized = Json.Serialize(parameters);

            xmlHttp.Send(serialized);
            return(await tcs.Task);
        }
コード例 #5
0
ファイル: JS.cs プロジェクト: Zaid-Ajaj/Bridge.Ractive
        public static void Get(GetConfig config)
        {
            var http = new XMLHttpRequest();

            http.Open("GET", config.Url, true);
            http.SetRequestHeader("Content-Type", "application/json");
            http.OnReadyStateChange = () =>
            {
                if (http.ReadyState == AjaxReadyState.Done && http.Status == 200)
                {
                    config.Success(http.ResponseText);
                }

                if (http.Status == 500 || http.Status == 404)
                {
                    config.Error();
                }
            };

            http.Send();
        }
コード例 #6
0
ファイル: JS.cs プロジェクト: Zaid-Ajaj/Bridge.Ractive
        /// <summary>
        /// Post's data to server
        /// </summary>
        /// <typeparam name="TData">The type of the data to send</typeparam>
        /// <typeparam name="TResult">The type of JSON.parse(http.ResponseText)</typeparam>
        /// <param name="config"></param>
        public static void PostJson <TData, TResult>(PostConfig <TData, TResult> config)
        {
            var http = new XMLHttpRequest();

            http.Open("POST", config.Url, true);
            http.SetRequestHeader("Content-Type", "application/json");
            http.OnReadyStateChange = () =>
            {
                if (http.ReadyState == AjaxReadyState.Done && http.Status == 200)
                {
                    var result = JSON.Parse(http.ResponseText);
                    config.Success(Script.Write <TResult>("result"));
                }

                if (http.Status == 500 || http.Status == 404)
                {
                    config.Error();
                }
            };

            var stringified = JSON.Stringify(config.Data);

            http.Send(stringified);
        }
コード例 #7
0
        public Task <HttpResponse> SendAsync(HttpRequestMethod method, string url, string body, string contentType,
                                             CancellationToken ct)
        {
            var tcs     = new TaskCompletionSource <HttpResponse>();
            var request = new XMLHttpRequest();

            request.OnReadyStateChange = () =>
            {
                if (request.ReadyState != AjaxReadyState.Done)
                {
                    return;
                }

                if ((request.Status >= 200 && request.Status < 300) || request.Status == 304)
                {
                    tcs.TrySetResult(new HttpResponse(true, request.Status, request.ResponseText));
                }
                else
                {
                    tcs.TrySetResult(new HttpResponse(false, request.Status));
                }
            };

            string methodStr;

            switch (method)
            {
            case HttpRequestMethod.Get:
                methodStr = "GET";
                break;

            case HttpRequestMethod.Post:
                methodStr = "POST";
                break;

            case HttpRequestMethod.Delete:
                methodStr = "DELETE";
                break;

            case HttpRequestMethod.Put:
                methodStr = "PUT";
                break;

            default:
                throw new ArgumentException("Unrecognized HTTP method.", nameof(method));
            }

            request.Open(methodStr, BaseUrl + url);
            if (!string.IsNullOrEmpty(contentType))
            {
                request.SetRequestHeader("Content-Type", contentType);
            }
            if (string.IsNullOrEmpty(body))
            {
                request.Send();
            }
            else
            {
                request.Send(body);
            }

            CancellationTokenRegistration reg = ct.Register(() =>
            {
                if (tcs.TrySetCanceled())
                {
                    request.Abort();
                }
            });

            tcs.Task.ContinueWith(_ => reg.Dispose());
            return(tcs.Task);
        }