[英]Sending data from JQuery to C#, ASP.Net
I have a canvas in my .aspx form page where one can sign a signature, I would like to send the base64 data from the JQuery client side to the C# Asp.Net side. 我在.aspx表单页中有一个画布,可以在其中签名。我想将base64数据从JQuery客户端发送到C#Asp.Net端。 Here I want to upload this base64 data to a database.
在这里,我想将此base64数据上传到数据库。
(a couple things I tried) Jquery: (我尝试过的几件事)jQuery:
$("#savebtn").bind("click", function () {
var base64 = $('#sketch')[0].toDataURL("image\png");
$.ajax({
url: 'EvaluatieForm.aspx', // page where i have button listenener
data: '{ data: "' + base64 + '"}',
type: 'POST',
async: true,
cache: false,
contentType: "application/json; charset=utf-8",
success: function (result) {
console.log("inside sucess");
console.log("result: " + result);
},
error: function (request, error) {
// This callback function will trigger on unsuccessful action
alert('Error!');
}
});
$.post("EvaluatieForm.aspx", { data: base64 }); // another thing i tried
C#: C#:
var d = Request.Params["data"];
The variable d is null when i put a breakpoint at it. 当我在其上设置断点时,变量d为null。 Does anybody see how I can tackle this hurdle, or make it easier?
有人看到我如何解决这个障碍,或者使它变得容易吗? (note: I do not have a lot of experience with JQuery)
(注意:我对JQuery没有太多的经验)
Your JSON with base64 could be available as request body. 带有base64的JSON可以作为请求正文使用。
using (StreamReader reader = new StreamReader(context.Request.InputStream))
{
string text = reader.ReadToEnd();
}
If you replace 如果您更换
url: 'EvaluatieForm.aspx'
by 通过
url: 'EvaluatieForm.aspx?data=' + base64
and remove 并删除
data: '{ data: "' + base64 + '"}',
then it will work. 那么它将起作用。
Try this: Just a small change in your existing code, used JSON.stringify to post data. 尝试以下操作:在现有代码中做一个很小的更改,使用JSON.stringify发布数据。
$("#savebtn").bind("click", function () {
var base64 = $('#sketch')[0].toDataURL("image\png");
var objectToPasss = {data: base64};
var postData =JSON.stringify(objectToPasss );
$.ajax({
url: 'EvaluatieForm.aspx', // page where i have button listenener
data: postData,
type: 'POST',
async: true,
cache: false,
contentType: "application/json; charset=utf-8",
success: function (result) {
console.log("inside sucess");
console.log("result: " + result);
},
error: function (request, error) {
// This callback function will trigger on unsuccessful action
alert('Error!');
}
});
$.post("EvaluatieForm.aspx", { data: base64 });
Try this: 尝试这个:
$("#savebtn").bind("click", function () {
$.ajax({
url: 'EvaluatieForm.aspx', // page where i have button listenener
data: {
sketch: $('#sketch')[0].toDataURL("image\png")
},
type: 'POST',
async: true,
cache: false,
contentType: "application/json; charset=utf-8",
success: function (result) {
console.log("inside sucess");
console.log("result: " + result);
},
error: function (request, error) {
// This callback function will trigger on unsuccessful action
alert('Error!');
}
});
where 哪里
var d = Request.Params["sketch"];
The data argument in the jQuery ajax() function works in conjunction with the contentType. jQuery ajax()函数中的data参数与contentType结合使用。 As you are setting it to application/json, then it stands to reason that data will be serialized, so setting the sketch variable seems about right.
当您将其设置为application / json时,则有理由将数据序列化,因此设置sketch变量似乎是正确的。
With ajax you can try xhr. 使用ajax,您可以尝试xhr。
maybe this thread helps you out! 也许这个线程可以帮助您! :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.