I had the same problem and solved it by adding type="button" attribute to the <button> element, by which IE thinks the button as a simple button instead of a submit button (which is default behavior of a <button> element).
Reference:
http://stackoverflow.com/questions/12325066/button-click-event-fires-when-pressing-enter-key-in-different-input-no-forms
Wednesday, September 21, 2016
A handy tool for writing RESTful API in golang. It enables you to exchange data between client and server through a uniform JSON format
A handy tool for writing RESTful API in golang. It enables you to exchange data between client and server through a uniform JSON format
iojson provides a convenient way to exchange data between your client and server through a uniform JSON format. It helps you to encode data from Go structs to a JSON string and to decode data from a JSON string to Go structs. iojson supports storing Go objects to a slice or to a map, which means you could reference your object either by a slice index or by a map key according to your preference. After populating data from JSON to Go objects, the methods of the objects remained working.
iojson also provides a HTTP middleware function, which works with Alice (a famous middleware chainer).
https://github.com/junhsieh/iojson
iojson provides a convenient way to exchange data between your client and server through a uniform JSON format. It helps you to encode data from Go structs to a JSON string and to decode data from a JSON string to Go structs. iojson supports storing Go objects to a slice or to a map, which means you could reference your object either by a slice index or by a map key according to your preference. After populating data from JSON to Go objects, the methods of the objects remained working.
iojson also provides a HTTP middleware function, which works with Alice (a famous middleware chainer).
https://github.com/junhsieh/iojson
Tuesday, September 20, 2016
invalid indirect of typedSlice (type interface {})
invalid indirect of typedSlice (type interface {})
You can't dereference typedSlice, because it's an interface{}. You would have to extract the pointer with a type assertion
realSlice := *typedSlice.(*[]Demo)
cannot range over typedSlice (type interface {})
Again, since typedSlice is an interface{}, you can't range over it. If you want to range over the values you need to use a type assertion, or iterate manually via reflect:
for i := 0; i < o.Elem().Len(); i++ {
ret = append(ret, o.Elem().Index(i).Interface())
}
Reference:
http://stackoverflow.com/questions/34163174/cant-use-range-on-slice-made-with-reflect-then-passed-json-unmarshal
Friday, September 16, 2016
HttpWebRequest cookie post header Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Net;
using System.Diagnostics;
namespace HttpWebRequestExample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
CookieContainer Cookie = null;
string CSRFtoken = "";
public MainWindow()
{
InitializeComponent();
}
private void LoginBtn_Click(object sender, RoutedEventArgs e)
{
Cookie = new CookieContainer(); // important
PerformRequest("POST", "https://erp.local:8443/Login", "{\"ObjArr\":[{\"Username\":\"bot\",\"Password\":\"bot\"}]}");
}
private void ItemBtn_Click(object sender, RoutedEventArgs e)
{
PerformRequest("GET", "https://erp.local:8443/Item/List", "");
}
private void SaveBtn_Click(object sender, RoutedEventArgs e)
{
PerformRequest("POST", "https://erp.local:8443/Ship/Save", "{\"Status\":true,\"ErrArr\":[],\"ErrCount\":0,\"ObjArr\":[{\"IDCNShip\":0,\"InvoiceNum\":15,\"ContainerNum\":\"\",\"ETD\":\"2016-09-13\",\"ETA\":\"\",\"IDWarehouse\":0,\"TotalQty\":0,\"IsClosed\":false,\"ShippedDate\":\"\",\"Remark\":\"\",\"Created\":\"\",\"Changed\":\"\",\"CNShipItemWalkerArr\":[{\"LineNum\":0,\"RowNum\":1,\"ItemNum\":\"A0\",\"ItemName\":\"\",\"SizeLong\":\"\",\"ColorShort\":\"\",\"ColorShortZH\":\"\",\"Qty\":0,\"NetWeight\":0,\"GrossWeight\":0,\"UnitPrice\":0,\"Remark\":\"\",\"Created\":\"\",\"Changed\":\"\"}],\"CNShipItemPartArr\":[{\"LineNum\":0,\"BoxLabel\":\"ABC\",\"ItemNum\":\"A1\"}]}],\"Data\":{}}");
}
private void PerformRequest(string method, string url, string postData)
{
// TODO: this is a temporary remove SSL ceritficate check solution. Please comment out this line in production.
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = Cookie; // use the global cookie variable
byte[] data = Encoding.UTF8.GetBytes(postData);
request.Method = method;
request.Headers.Add("X-CSRF-Token", CSRFtoken);
if (method == "POST")
{
request.ContentType = "application/json; charset=utf-8";
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
WebResponse response = (HttpWebResponse)request.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
CSRFtoken = response.Headers.Get("X-CSRF-Token");
Debug.WriteLine("CSRFtoken: " + CSRFtoken);
Debug.WriteLine(responseString);
}
public bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
}
}
}
Reference:
http://csharp-tricks-en.blogspot.ca/2015/03/http-requests-get-or-post-with-cookies.html
Wednesday, September 14, 2016
Convert JSON string to C# object
Convert JSON string to C# object
Reference:
http://json2csharp.com/
using System.Windows;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
namespace WpfApplication4
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
string str = "{\"Status\":true,\"ErrArr\":[],\"ErrCount\":0,\"ObjArr\":[],\"Data\":{\"Test\":\"Hello\",\"ItemArr\":[{\"IDItem\":111,\"ItemNum\":\"AA01\",\"ItemName\":\"Computer\"},{\"IDItem\":112,\"ItemNum\":\"AA02\",\"ItemName\":\"Desk\"}]}}";
MemoryStream jsonSource = new MemoryStream(Encoding.UTF8.GetBytes(str));
var s = new DataContractJsonSerializer(typeof(RootObject));
var j = (RootObject)s.ReadObject(jsonSource);
Debug.WriteLine(str);
Debug.WriteLine(j.Data.Test);
for (int i = 0; i < j.Data.ItemArr.Count; i++)
{
Debug.WriteLine(j.Data.ItemArr[i].IDItem + "; " + j.Data.ItemArr[i].ItemNum + "; " + j.Data.ItemArr[i].ItemName);
}
}
}
[DataContract]
public class ItemArr
{
[DataMember]
public int IDItem { get; set; }
[DataMember]
public string ItemNum { get; set; }
[DataMember]
public string ItemName { get; set; }
}
[DataContract]
public class Data
{
[DataMember]
public string Test { get; set; }
[DataMember]
public List<ItemArr> ItemArr { get; set; }
}
[DataContract]
public class RootObject
{
[DataMember]
public bool Status { get; set; }
[DataMember]
public List<object> ErrArr { get; set; }
[DataMember]
public int ErrCount { get; set; }
[DataMember]
public List<object> ObjArr { get; set; }
[DataMember]
public Data Data { get; set; }
}
}
Reference:
http://json2csharp.com/
Subscribe to:
Posts (Atom)