WCF从json转移空值的问题 [英] Problem with WCF getting null value transfered from json
问题描述
我想要创建一个与wcf和json相关的项目但是每当我运行这段代码时,我都会在wcf中获得null值。向我展示我的代码plz检查并纠正我,如果我错了
wcf代码
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Runtime.Serialization;
使用System.ServiceModel;
使用System.ServiceModel.Activation;
使用System.ServiceModel.Web;
使用System.Text;
使用System.Configuration;
使用System.Data;
使用System.Data.SqlClient;
[ServiceContract(Namespace =)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
公共课 EmployeeService
{
[OperationContract]
[System.ServiceModel.Web.WebInvoke(Method =POST) ,BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
公共员工 GetEmployeeById (字符串employeeId)
{
员工员工=新员工( );
string cs = ConfigurationManager.ConnectionStrings [conn]。ConnectionString;
string query =select top 1 id,empname,address,basicsalary from employee where id = @ id;
使用(SqlConnection con = new SqlConnection(cs))
{
con.Open();
SqlCommand cmd = new SqlCommand(query,con);
//cmd.CommandType = CommandType.StoredProcedure;
SqlParameter paramid = new SqlParameter();
paramid.ParameterName =@ id;
paramid.Value = employeeId;
cmd.Parameters.Add(paramid);
SqlDataReader rdr = cmd.ExecuteReader();
while(rd r.Read())
{
employee.name = rdr [empname]。ToString();
employee.address = rdr [address]。ToString();
employee.basicSalary = rdr [basicsalary]。ToString();
}
返回员工;
}
}
//添加更多在这里进行操作并用[OperationContract]标记它们
}
我尝试了什么:
html代码
<%@ Page Language =C#AutoEventWireup = trueCodeFile =Default2.aspx.csInherits =Default2%>
<!DOCTYPE html>
< br $>
< title>
$(document).ready(function(){
$(#btnGetEmployee)。click(function(){
var empid = $(#txtid);
var employeeId = empid.val();
$ .ajax({
// url:'EmployeeService.svc / GetEmployeeById',
url:'http:// localhost:29764 / EmployeeService.svc / GetEmployeeById',
类型:'POST',
contentType:application / json; charset = utf-8,
数据:JSON.stringify({GetEmployeeById:employeeId}),
//数据:{'GetEmployeeById':'+ employeeId +'},
dataType:'json',
成功:函数(数据){
$('#txtName') .val(data.d.name);
$('#txtAddress')。val(data.d.address);
$('#txtbasicSalary') .val(data.d.basicSalary);
},
错误:函数(错误){
alert(empid);
}
});
});
});
<% -
$(文件).ready(function(){
$('#& lt;%= btnGetEmployee.ClientID%>')。click(function(){
alert(.click()处理程序调用。);
});
});
- %>
I am tying to make a project related to wcf and json but whenever I run this code I am getting null value in wcf. showing you my code plz check and correct me if I am wrong
wcf code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class EmployeeService
{
[OperationContract]
[System.ServiceModel.Web.WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
public Employee GetEmployeeById(string employeeId)
{
Employee employee = new Employee();
string cs = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
string query = "select top 1 id, empname,address,basicsalary from employee where id=@id";
using (SqlConnection con = new SqlConnection(cs))
{
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
//cmd.CommandType = CommandType.StoredProcedure;
SqlParameter paramid = new SqlParameter();
paramid.ParameterName = "@id";
paramid.Value = employeeId;
cmd.Parameters.Add(paramid);
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
employee.name = rdr["empname"].ToString();
employee.address = rdr["address"].ToString();
employee.basicSalary = rdr["basicsalary"].ToString();
}
return employee;
}
}
// Add more operations here and mark them with [OperationContract]
}
What I have tried:
html code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html>
<title>
$(document).ready(function () {
$("#btnGetEmployee").click(function () {
var empid = $("#txtid");
var employeeId = empid.val();
$.ajax({
//url: 'EmployeeService.svc/GetEmployeeById',
url: 'http://localhost:29764/EmployeeService.svc/GetEmployeeById',
type: 'POST',
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ GetEmployeeById: employeeId }),
//data: "{'GetEmployeeById':'" + employeeId + "'}",
dataType: 'json',
success: function (data) {
$('#txtName').val(data.d.name);
$('#txtAddress').val(data.d.address);
$('#txtbasicSalary').val(data.d.basicSalary);
},
error: function (err) {
alert(empid);
}
});
});
});
<%--
$(document).ready(function () {
$('#<%= btnGetEmployee.ClientID %>').click(function () {
alert("Handler for .click() called.");
});
});
--%>
ID:
< % - < asp:Button ID =btnGetEmployeerunat =serverText =Button/> - %>
姓名:
地址:
基本工资:
ID:
<%--<asp:Button ID="btnGetEmployee" runat="server" Text="Button" />--%>
name:
Address:
Basic Salary:
推荐答案
(document).ready(function(){
(document).ready(function () {
(#btnGetEmployee)。click(function(){
var empid =
("#btnGetEmployee").click(function () {
var empid =
(#txtid);
var employeeId = empid.val();
("#txtid");
var employeeId = empid.val();
这篇关于WCF从json转移空值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!