/// <summary> /// Removes a converter (to sanatize the initial response) from the list of converters. /// </summary> /// <param name="type">The type which uses this converter.</param> /// <returns>The current instance.</returns> public AjaxRequestOptions RemoveConverter(AjaxDataType type) { if (filters.ContainsKey(type)) { filters.Remove(type); } return(this); }
/// <summary> /// Adds a converter (to sanatize the initial response) for a given type to the list of converters. /// </summary> /// <param name="type">The type which should use the converter.</param> /// <param name="function">The delegate to your converter.</param> /// <returns>The current instance.</returns> public AjaxRequestOptions AddConverter(AjaxDataType type, AjaxFilterHandler function) { if (filters.ContainsKey(type)) { filters[type] = function; } else { filters.Add(type, function); } return(this); }
public Select2AjaxOption DataType(AjaxDataType value) { Attributes["dataType"] = string.Format("'{0}'", value.ToString().ToLower()); return(this); }
/// <summary> /// Removes a converter (to sanatize the initial response) from the list of converters. /// </summary> /// <param name="type">The type which uses this converter.</param> /// <returns>The current instance.</returns> public AjaxRequestOptions RemoveConverter(AjaxDataType type) { if (filters.ContainsKey(type)) filters.Remove(type); return this; }
/// <summary> /// Adds a converter (to sanatize the initial response) for a given type to the list of converters. /// </summary> /// <param name="type">The type which should use the converter.</param> /// <param name="function">The delegate to your converter.</param> /// <returns>The current instance.</returns> public AjaxRequestOptions AddConverter(AjaxDataType type, AjaxFilterHandler function) { if (filters.ContainsKey(type)) filters[type] = function; else filters.Add(type, function); return this; }
/// <summary> /// 设置服务器端返回异步内容的数据类型 /// </summary> /// <param name="dataType">服务器端返回异步内容的数据类型</param> public AjaxFormOptions SetDataType(AjaxDataType? dataType) { this.DataType = dataType; return this; }
object CreateObject(AjaxDataType dt) { if (Options.ProcessData) { var methods = GetType().GetMethods(); foreach (var method in methods) if (method.Name.Equals("Create" + dt.ToString() + "Object")) return method.Invoke(this, null); } return ResponseText; }
protected object GetResultFromResponse(HttpWebResponse response, AjaxDataType dataType) { object resultObject = null; if (response.StatusCode == HttpStatusCode.OK) { using (var stream = response.GetResponseStream()) { var resultData = stream.ToByteArray(); var result = Encoding.UTF8.GetString(resultData); var success = false; //If there is no contents, return undefined. if (resultData.Length == 0) { resultObject = Undefined.Value; success = true; } if (dataType == AjaxDataType.Unknown || dataType == AjaxDataType.Json) { //Attempt to convert the result into Json if (!success) { try { resultObject = JSONObject.Parse(Engine, result, null); success = true; } catch { /* Do Nothing. */ } } } if (dataType == AjaxDataType.Unknown || dataType == AjaxDataType.Xml) { if (!success) { //Attempt to convert the result into Xml try { XDocument doc; using (var xmlStream = new MemoryStream(resultData)) { using (var xmlReader = new XmlTextReader(xmlStream)) { doc = XDocument.Load(xmlReader); } } var jsonDocument = JsonConvert.SerializeXmlNode(doc.Root.GetXmlNode()); resultObject = JSONObject.Parse(Engine, jsonDocument, null); success = true; } catch { /* Do Nothing. */ } } } if (dataType == AjaxDataType.Unknown || dataType == AjaxDataType.Raw) { if (!success) { //If we couldn't convert as json or xml, use it as a byte array. resultObject = new Base64EncodedByteArrayInstance(Engine.Object.InstancePrototype, resultData); } } } } else { resultObject = String.Format("Error attempting to retrieve {2}: {0} {1}", response.StatusCode, response.StatusDescription, response.ResponseUri); } return(resultObject); }