Trying to pass json data in stringify formatv

问题内容:

I’m trying to pass JSON data in stringify format through Ajax.below is the ajax part

$.ajax({
    type: "POST",
    url: "TDC.aspx/SaveFrmDetails",
    data: '{user: "' + JSON.stringify(user) + '",byteData: "' + byteData + '", imageName: "' + fileName + '", contentType: "' + contentType + '" }',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {
        alert("User has been added successfully.");
        window.location.reload();
    }
});

and at server side It is received like this
Properties of user to hold

public class User
{
    public decimal PRODUCT_ID { get; set; }
    public string TDC_NO { get; set; }
    public decimal REVISION { get; set; }
    public DateTime REVISION_DATE { get; set; }
    public string P_GROUP { get; set; }
    public string PROD_DESC { get; set; }
    public string N_I_PRD_STD { get; set; }
    public string APPLN { get; set; }
    public string FRM_SUPP { get; set; }
    public DateTime CREATED_DATE { get; set; }
    public string CREATED_BY { get; set; }
}

Web method

[WebMethod]    
    public static void SaveFrmDetails(User user, string byteData, string imageName, string contentType)
    {
        byte[] bytes = Convert.FromBase64String(byteData);
        string connectionString = ConfigurationManager.ConnectionStrings["connw"].ConnectionString;
 using (OracleConnection con = new OracleConnection(connectionString))
        {
            using (OracleCommand cmd = new OracleCommand("INSERT INTO TDC_PRODUCT1(PRODUCT_ID,TDC_NO, REVISION,REVISION_DATE,P_GROUP,PROD_DESC,N_I_PRD_STD,APPLN,FRM_SUPP,CREATED_DATE,CREATED_BY,NAME,CONTENTTYPE,DATA ) VALUES (:PRODUCT_ID,:TDC_NO,:REVISION,:REVISION_DATE,:P_GROUP,:PROD_DESC,:N_I_PRD_STD,:APPLN,:FRM_SUPP,:CREATED_DATE,:Created_By,:NAME,:CONTENTTYPE,:DATA)", con))
            {                
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue(":PRODUCT_ID", user.PRODUCT_ID);
                cmd.Parameters.AddWithValue(":TDC_NO", user.TDC_NO);
                cmd.Parameters.AddWithValue(":REVISION", user.REVISION);
                cmd.Parameters.AddWithValue(":REVISION_DATE", user.REVISION_DATE);
                cmd.Parameters.AddWithValue(":P_GROUP", user.P_GROUP);
                cmd.Parameters.AddWithValue(":PROD_DESC", user.PROD_DESC);
                cmd.Parameters.AddWithValue(":N_I_PRD_STD", user.N_I_PRD_STD);
                cmd.Parameters.AddWithValue(":APPLN", user.APPLN);
                cmd.Parameters.AddWithValue(":FRM_SUPP", user.FRM_SUPP);
                cmd.Parameters.AddWithValue(":CREATED_DATE", user.CREATED_DATE);
                cmd.Parameters.AddWithValue(":CREATED_BY", user.CREATED_BY);
                cmd.Parameters.AddWithValue(":NAME", imageName);
                cmd.Parameters.AddWithValue(":CONTENTTYPE", contentType);
                cmd.Parameters.AddWithValue(":DATA", bytes);    
cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }          

Is the above mention style is correct way to do or any modification is needed.

问题评论:

    
Try once data: JSON.stringify({ user: user, byteData: byteData, imageName: fileName, contentType: contentType })
    
@Satpal Thx it’s working..
– hari
5 hours ago

原文地址:

https://stackoverflow.com/questions/47751024/trying-to-pass-json-data-in-stringify-formatv

添加评论

友情链接:蝴蝶教程