here is my code:
Code: Select all
public static class SignalGoHelper
{
public static string HostUrl { get; set; } = "http://host.com/";
public static void PostData<T>(string url, Action<T> response, params Tuple<string, string>[] parameters) where T : class, new()
{
url = HostUrl + url;
Action<object> callback = (result) =>
{
response(Response<T>(result.ToString()));
};
CSHTML5.Interop.ExecuteJavaScript(@"var formData = new FormData();
eval($1);
jQuery.ajax({
url : $0,
method: 'POST',
xhrFields: { withCredentials: true },
dataType: 'text',
crossDomain: true,
data: formData,
processData: false,
contentType: false,
success: function(data, textStatus, jqXHR){
$2(data);
},
error: function(jqXHR, textStatus, errorThrown){
//if fails
}
});", url, CalculateParameters(parameters), callback);
}
static string CalculateParameters(Tuple<string, string>[] parameters)
{
StringBuilder result = new StringBuilder();
foreach (var item in parameters)
{
result.AppendLine($"formData.append('{item.Item1}', '{item.Item2}');");
}
return result.ToString();
}
static T Response<T>(string data) where T : class, new()
{
try
{
var msg = JsonConvert.DeserializeObject<T>(data, true);
return msg.Result;
}
catch (Exception ex)
{
return null;
}
}
}