@@ -3,6 +3,7 @@
3module Reflex.Dom.Xhr
3module Reflex.Dom.Xhr
4 ( module Reflex.Dom.Xhr
4 ( module Reflex.Dom.Xhr
5 , XMLHttpRequest
5 , XMLHttpRequest
6 , responseTextToText
7 , responseTextToText
7 , xmlHttpRequestGetReadyState
8 , xmlHttpRequestGetReadyState
8 , xmlHttpRequestGetResponseText
9 , xmlHttpRequestGetResponseText
@@ -55,11 +56,20 @@ data XhrRequestConfig
55 deriving (Show, Read, Eq, Ord, Typeable)
56 deriving (Show, Read, Eq, Ord, Typeable)
57data XhrResponse
58data XhrResponse
58− = XhrResponse { _xhrResponse_body :: Maybe Text
59− , _xhrResponse_status :: Word
59+ = XhrResponse { _xhrResponse_status :: Word
60 , _xhrResponse_statusText :: Text
60 , _xhrResponse_statusText :: Text
61+ , _xhrResponse_response :: Maybe XhrResponseBody
62+ , _xhrResponse_responseText :: Maybe Text
62− deriving (Show, Read, Eq, Ord, Typeable)
64+ deriving (Eq, Typeable)
66+{-# DEPRECATED _xhrResponse_body "Use _xhrResponse_response or _xhrResponse_responseText instead." #-}
67+_xhrResponse_body :: XhrResponse -> Maybe Text
68+_xhrResponse_body = _xhrResponse_responseText
70+{-# DEPRECATED xhrResponse_body "Use xhrResponse_response or xhrResponse_responseText instead." #-}
71+xhrResponse_body :: Lens XhrResponse XhrResponse (Maybe Text) (Maybe Text)
72+xhrResponse_body = lens _xhrResponse_responseText (\r t -> r { _xhrResponse_responseText = t })
64instance Default XhrRequestConfig where
74instance Default XhrRequestConfig where
65 def = XhrRequestConfig { _xhrRequestConfig_headers = Map.empty
75 def = XhrRequestConfig { _xhrRequestConfig_headers = Map.empty
@@ -80,6 +90,7 @@ newXMLHttpRequest req cb = do
80 liftIO $ do
90 liftIO $ do
81 xhr <- xmlHttpRequestNew wv
91 xhr <- xmlHttpRequestNew wv
82 let c = _xhrRequest_config req
92 let c = _xhrRequest_config req
93+ responseType = _xhrRequestConfig_responseType c
83 xmlHttpRequestOpen
94 xmlHttpRequestOpen
85 (_xhrRequest_method req)
96 (_xhrRequest_method req)
@@ -95,8 +106,15 @@ newXMLHttpRequest req cb = do
95 statusText <- liftIO $ xmlHttpRequestGetStatusText xhr
106 statusText <- liftIO $ xmlHttpRequestGetStatusText xhr
96 if readyState == 4
107 if readyState == 4
109+ t <- if responseType == Just "text" || responseType == Nothing
98− r <- liftIO $ xmlHttpRequestGetResponseText xhr
110+ then liftIO $ xmlHttpRequestGetResponseText xhr
112+ r <- liftIO $ xmlHttpRequestGetResponse xhr
99− _ <- liftIO $ postGui $ cb $ XhrResponse (responseTextToText r) status (statusTextToText statusText)
113+ _ <- liftIO $ postGui $ cb $ XhrResponse { _xhrResponse_status = status
114+ , _xhrResponse_statusText = statusTextToText statusText
115+ , _xhrResponse_response = r
116+ , _xhrResponse_responseText = responseTextToText t
100 return ()
118 return ()
101 else return ()
119 else return ()
102 _ <- xmlHttpRequestSend xhr (_xhrRequestConfig_sendData c)
120 _ <- xmlHttpRequestSend xhr (_xhrRequestConfig_sendData c)
@@ -143,5 +161,5 @@ decodeText = decode . BL.fromStrict . encodeUtf8
144-- | Convenience function to decode JSON-encoded responses.
162-- | Convenience function to decode JSON-encoded responses.
145decodeXhrResponse :: FromJSON a => XhrResponse -> Maybe a
163decodeXhrResponse :: FromJSON a => XhrResponse -> Maybe a
146−decodeXhrResponse = join . fmap decodeText . _xhrResponse_body
164+decodeXhrResponse = join . fmap decodeText . _xhrResponse_responseText