0
我從聯機服務器調用json數據而不是本地主機時收到[對象對象]警報。它在localhost上完美運行。這裏的ASPX代碼我在調用json數據時收到[對象對象]警報
$(document).ready(function() {
$('#btnGetEmployee').click(function() {
var empId = $('#txtId').val();
$.ajax({
url: 'WebForm1.aspx/GetEmployeeById',
method: 'post',
contentType: "application/json",
data: '{employeeId:' + empId + '}',
dataType: "json",
success: function (data) {
$('#txtName').val(data.d.Name);
$('#txtGender').val(data.d.Gender);
$('#txtSalary').val(data.d.Salary);
},
error: function (err) {
alert(err);
}
});
});
});
和這裏的背後
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace Demo
{
public partial class WebForm1 : System.Web.UI.Page
{
[System.Web.Services.WebMethod]
public static Employee GetEmployeeById(int employeeId)
{
Employee employee = new Employee();
string cs = ConfigurationManager.ConnectionStrings
["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spGetEmployeeById", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter()
{
ParameterName = "@Id",
Value = employeeId
});
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
employee.ID = Convert.ToInt32(rdr["Id"]);
employee.Name = rdr["Name"].ToString();
employee.Gender = rdr["Gender"].ToString();
employee.Salary = Convert.ToInt32(rdr["Salary"]);
}
}
return employee;
}
}
}
的代碼肯定有一些行沒有考慮網上的服務器我希望有經驗的人會回答我。 非常感謝
如果您使用'console.log'而不是'alert',您將能夠在控制檯中看到錯誤。 – Turnip
@Turnip我得到這個錯誤 狀態文本 : 「內部服務器錯誤」 –