jquery - Ajax Post 和 Asp.Net webApplication
问题描述
我有问题,我的代码不起作用!它给了我一个通用的服务器错误(500)。我不明白出了什么问题;我尝试使用调试进行检查,当它进入 ajax 时会出现错误。请检查代码:这是前端:
<asp:Content ID="Content2"
ContentPlaceHolderID="ContentPlaceHolderMainBody" runat="server">
<form onsubmit="Dati()" action="InterventionsManagerNew.aspx">
<div id="container">
<div id="signature"></div>
<div id="Pulsanti">
<input id="Button1" type="button" value="Salva" onclick="Dati()"/>
<input id="Button5" type="button" value="Pulisci"
onclick="Reset()" />
</div>
</div>
</form>
</asp:Content>
JS:
function Dati() {
var $sigdiv = $("#signature");
var datapair = $sigdiv.jSignature("getData", "base30");
var i = new Image();
var s = "data:" + datapair[0] + "," + datapair[1];
var Dato = { "userdata": s };
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "InterventionsManagerSignature.aspx/Firma",
data: JSON.stringify(Dato),
datatype: "json",
success: function (result) {
//do something
alert("SUCCESS = " + result.d);
// close_window();
console.log(result);
},
error:function(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
};
在后端我有:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Firma(string userdata)
{
return "Ciao, " + userdata;
}
它每次都给我同样的错误而不会触发回发 webmethod 请帮助我理解我错了!!!
解决方案
Fabio,我的评论在代码中。归功于https://willowsystems.github.io/jSignature/#/about/
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="InterventionsManagerSignature.aspx.cs" Inherits="FredWebForm.InterventionsManagerSignature" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jSignature/2.1.3/jSignature.min.js"></script>
<script type="text/javascript">
var i = new Image();
$(function () {
var $sigdiv = $("#signature")
$sigdiv.jSignature() // inits the jSignature widget.
// after some doodling...
$sigdiv.jSignature("reset") // clears the canvas and rerenders the decor on it.
// Getting signature as SVG and rendering the SVG within the browser.
// (!!! inline SVG rendering from IMG element does not work in all browsers !!!)
// this export plugin returns an array of [mimetype, base64-encoded string of SVG of the signature strokes]
var datapair = $sigdiv.jSignature("getData", "svgbase64")
//var i = new Image()
i.src = "data:" + datapair[0] + "," + datapair[1]
$(i).appendTo($("#someelement")) // append the image (SVG) to DOM.
// Getting signature as "base30" data pair
// array of [mimetype, string of jSIgnature"s custom Base30-compressed format]
datapair = $sigdiv.jSignature("getData", "base30")
// reimporting the data into jSignature.
// import plugins understand data-url-formatted strings like "data:mime;encoding,data"
$sigdiv.jSignature("setData", "data:" + datapair.join(","))
})
function Dati() {
var Dato = { "userdata": i.src };
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "InterventionsManagerSignature.aspx/Firma",
data: JSON.stringify(Dato),
datatype: "json",
success: function (result) {
//do something
alert("SUCCESS = " + result.d);
// close_window();
console.log(result);
},
error: function (jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
};
</script>
<%--Fabio, the form tag is in the master page--%>
<div id="container">
<div id="signature" style="border: 5px solid black;"></div>
<div id="someelement"></div>
<div id="Pulsanti">
<input id="Button1" type="button" value="Salva" onclick="Dati()" />
<input id="Button5" type="button" value="Pulisci"
onclick="Reset()" />
</div>
</div>
</asp:Content>
这是后面的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FredWebForm
{
public partial class InterventionsManagerSignature : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
//Fabio this method needs to be static
public static string Firma(string userdata)
{
return "Ciao, " + userdata;
}
}
}
推荐阅读
- asp.net-mvc - 编辑级联下拉列表asp.net mvc 5
- c# - 新的 C# 8.0 开关表达式的运算符优先级是什么?
- python - Plotly:如何创建一个没有标题的表格?
- javascript - 节点服务器:由于不允许的 MIME 类型(“text/html”),加载模块被阻止
- angular - 无法读取未定义的“管道”
- javascript - 在网页中的 .pdf 或图像文件上绘制点
- python - 如何修复太多 If return 使代码过于复杂
- c# - 我如何将 someting.split() 的一部分存储到它自己的数组中?
- ruby-on-rails - `rake assets:precompile` 在生产中时出现`babel-loader` 依赖错误
- aws-lambda - 编排 GCP 云功能