执行时,jquery ajax代码不会执行

时间:2015-01-28 07:29:20

标签: javascript c# jquery asp.net ajax

我有一个使用jquery ajax的场景,插入发生在数据库中。发生的问题是,当我运行并调试网站时,它甚至不会调用jquery ajax代码。

sample.aspx代码:

<form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <script type="text/javascript" lang="javascript">
            function insert() {
                alert("hello");
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "sample.aspx/ins",
                    dataType: "json",
                    data: "{'rfrnc':'" + $("#<%=TextBoxReference.ClientID%>").val() +
                           "','picpost':'" + $("<%=TextBoxPicPostCode.ClientID%>").val() +
                           "','picadd':'" + $("<%=TextBoxPicAddress.ClientID%>").val().replace("'", "~") +
                           "','despost':'" + $("<%=TextBoxDestPostCode.ClientID%>").val() +
                           "','desadd':'" + $("<%=TextBoxDestAddress.ClientID%>").val().replace("'", "~") +
                           "','distance':'" + $("<%=TextBoxDistance.ClientID%>").val() + "'}",
                async: true,
                success: function (data) {
                    var res = data.d;
                    //alert(res);
                    $("<%=Result.ClientID%>").val(res);
                    },
                error: function (result) {
                    alert(result);
                }

            });
            }
        </script>


        <div>
            <asp:TextBox ID="TextBoxReference" Enabled="false" CssClass="time" runat="server"></asp:TextBox><br />
            <asp:TextBox ID="TextBoxPicPostCode" Text="SW3 4BN" CssClass="time" runat="server"></asp:TextBox><br />
            <asp:TextBox ID="TextBoxPicAddress" Text="Radnor Walk Chelsea, London, United Kingdom" CssClass="address" runat="server"></asp:TextBox><br />
            <asp:TextBox ID="TextBoxDestPostCode" Text="SW3 2RZ" CssClass="time" runat="server"></asp:TextBox><br />
            <asp:TextBox ID="TextBoxDestAddress" Text="Spanish Consulate 20 Draycott Place, London, United Kingdom" CssClass="address" runat="server"></asp:TextBox><br />
            <asp:TextBox ID="TextBoxDistance" Text="10" onblur="insert();" CssClass="time" runat="server"></asp:TextBox><br />               
            <asp:TextBox ID="Result" CssClass="time" runat="server"></asp:TextBox>
        </div>
    </form>

sample.cs代码:

protected void Page_Load(object sender, EventArgs e)
    {
        using (Estb_cntxtDataContext cabeeDB = new Estb_cntxtDataContext())
        {
            var obj_tblJob = (from a in cabeeDB.Jobs where a.nCompanyID == 5 select a.Job_No).Max();
            int job_no = Convert.ToInt32(obj_tblJob + 1);
            TextBoxReference.Text = "CRC-" + job_no.ToString(); 
        }
    }   

    [WebMethod]
    public static string ins(string rfrnc, string picpost, string picadd, string despost, string desadd, string distance)
    {
        using(Estb_cntxtDataContext context=new Estb_cntxtDataContext())
        {
            picadd = picadd.Replace("~", "'");
            desadd = desadd.Replace("~", "'");

            Job j = new Job();

            j.CustType = "E";
            j.Customer_ID = 321321;
            j.Job_Booking_DateTime = DateTime.Now;
            j.Job_Execution_DateTime = DateTime.Now;
            j.Job_No = Convert.ToDecimal(rfrnc.Substring(rfrnc.LastIndexOf('-') + 1));
            j.strPickUpPostCode = picpost;
            j.House_No = "10";
            j.strPickUpAddress = picadd;
            j.Name = "Haider Khattak";
            j.strDestinationPostCode = despost;
            j.DestinationHouse_No = "20";
            j.strDestinationAddress = desadd;
            j.Ctype = "std";
            j.Commission = 10;
            j.Price = 20;
            j.strNote = "Testing....";
            j.strJobStatus = "Processing";
            j.bIsJobConfirmed = false;
            j.Miles = Convert.ToDouble(distance);
            j.dtDriverAssignedDateTime = null;
            j.nCompanyID = 5;
            j.Devicetype = "DeskTop";
            j.Passenger = "0";
            j.Luggage = "0";
            j.Credit_Card = false;            
            j.Invoice_Del = false;
            j.Invoice_Gen = false;
            j.Commission_Gen = false;

            try
            {
                context.Jobs.InsertOnSubmit(j);
                context.SubmitChanges();
                return "Saved";
            }

            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }

0 个答案:

没有答案