Ajax Post和Asp.Net webApplication

时间:2019-03-18 17:12:26

标签: jquery asp.net ajax web-applications

我遇到了问题,我的代码无法正常工作! 它给了我一个一般性的服务器错误(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;
    }

它每次都给我同样的错误,而不会触发postaback webmethod 请帮助我了解我的错!!

1 个答案:

答案 0 :(得分:0)

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;
        }
    }
}