使用Ajax Jquery和asmx服務通過Id獲取數據

[英]Getting Data By Id with Ajax Jquery and asmx service


I want to get selected Employee data by Id so a modal will pop up and show data for this Id. The Id parses as a parameter but asmx service won't fire at last. Here is client side code :

我想通過Id獲取選定的員工數據,這樣就會彈出一個模式並顯示此Id的數據。客戶端代碼如下:

 function getEmployeebyID(Id) {
        $("#ModalTitle").html("Update Employee");
        $("#MyModal").modal();
        var Id = Id;

        $.ajax({
            url: "EmployeeService.asmx/GetEmployeeById",
            type: "GET",              

            dataType: "json",
            data: { Id: Id },
            success: function (data) {


                $('#txtFirstName').val(data[0].FirstName);
                $('#txtLastName').val(data[0].LastName);
                $('#txtGender').val(data[0].Gender);
                $('#txtJobTitle').val(data[0].JobTitle);
                $('#txtSalary').val(data[0].Salary);
                $('#txtHireDate').val(data[0].HireDate);


            },
            error: function (errormessage) {
                alert(errormessage.responseText);
            }
        });
        return false;
    }

Here is server side code :

以下是服務器端代碼:

[WebMethod]

    public void GetEmployeeById(int Id)  
    {
        Employee employee = new Employee();

        string cs = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("spGetEmployeeById", con);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlParameter parameter = new SqlParameter();
            parameter.ParameterName = "@Id";
            parameter.Value = Id;

            cmd.Parameters.Add(parameter);
            con.Open();
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                employee.Id = Convert.ToInt32(rdr["Id"]);
                employee.FirstName = rdr["FirstName"].ToString();
                employee.LastName = rdr["LastName"].ToString();
                employee.Gender = rdr["Gender"].ToString();
                employee.JobTitle = rdr["JobTitle"].ToString();
                employee.Salary = Convert.ToInt32(rdr["Salary"]);
                employee.HireDate = Convert.ToDateTime(rdr["HireDate"]);
            }
        }

        JavaScriptSerializer js = new JavaScriptSerializer();
        Context.Response.Write(js.Serialize(employee));
    }

Server side code is working when I invoke data when I am running it,so the problem is on client side and throws this: request format is unrecognized for url unexpectedly ending in /GetEmployeeById As far as I inspected elements I saw this error : localhost:50791/Services/EmployeeService.asmx/GetEmployeeById?Id=42 500 (Internal Server Error). Can't figure out what's going on...

當我運行它時,當我調用數據時,服務器端代碼正在工作,所以問題是在客戶端並拋出這個:請求格式在我所檢查的元素中意外地結束在/GetEmployeeById中沒有被識別,我看到了這個錯誤:localhost:50791/Services/EmployeeService.asmx/GetEmployeeById?Id= 42500(內部服務器錯誤)。不知道發生了什么……

2 个解决方案

#1


1  

Try this

試試這個

 function getEmployeebyID(Id) {
        $("#ModalTitle").html("Update Employee");
        $("#MyModal").modal();
    var Emp={
    Id = Id
    }
        $.ajax({
            url: "../EmployeeService.asmx/GetEmployeeById",
            type: "POST",              
            contentType : "application/json; charset=utf-8",     
            dataType: "json",
            data: JSON.stringify(Emp),
            success: function (data) {


                $('#txtFirstName').val(data[0].FirstName);
                $('#txtLastName').val(data[0].LastName);
                $('#txtGender').val(data[0].Gender);
                $('#txtJobTitle').val(data[0].JobTitle);
                $('#txtSalary').val(data[0].Salary);
                $('#txtHireDate').val(data[0].HireDate);


            },
            error: function (errormessage) {
                alert(errormessage.responseText);
            }
        });
        return false;
    }

#2


1  

Finally from this How do I fix a "Request format is unrecognized for URL..." error in a web service running in IIS? I added on web.config :

最后,我如何修正在IIS中運行的web服務中的“請求格式不被確認為URL…”的錯誤?我添加了網絡。配置:

 <webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>
</webServices>

And removed from

且遠離

 $('#txtFirstName').val(data[0].FirstName);

the [0] and all worked fine.

[0]和所有的工作都很順利。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/03/05/7301cab49ea907609cfe0bdb98dc0785.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com