Reactjs 对wcf web服务响应axios post请求
我试图将数据从react应用程序发布到wcf服务类。这是一个简单的对象,只有名字、年龄和性别。正在从react应用程序调用服务,但没有数据。如果使用get请求和参数传递数据,则数据将传递给服务,但我希望使用post请求。是否需要匹配wcf服务类中的字段名 为了在react组件中进行测试,我使用了以下代码Reactjs 对wcf web服务响应axios post请求,reactjs,api,class,object,wcf,Reactjs,Api,Class,Object,Wcf,我试图将数据从react应用程序发布到wcf服务类。这是一个简单的对象,只有名字、年龄和性别。正在从react应用程序调用服务,但没有数据。如果使用get请求和参数传递数据,则数据将传递给服务,但我希望使用post请求。是否需要匹配wcf服务类中的字段名 为了在react组件中进行测试,我使用了以下代码 const headers = { 'content-type': 'application/json' } const puser = {
const headers = {
'content-type': 'application/json'
}
const puser = {
Name:'Test',
Age:'23',
Gender:'F',
}
axios.post('http://localhost:3292/myService.svc/adduser',{headers:headers}, puser)
.then(res=> {
console.log(res.data)
})
在wcf服务中,类代码为
public class UserClass
{
string name;
int age;
string gender;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
public string Gender
{
get
{
return gender;
}
set
{
gender = value;
}
}
}
服务电话
public string AddUser(List<User> U)
{
UserClass usr = new UserClass();
return "success";
}
公共字符串AddUser(列表U)
{
UserClass usr=新的UserClass();
返回“成功”;
}
Axios post方法期望将数据
作为第二个参数。尝试用{headers}
交换puser
。
不要在axios.post中添加标题,代码应如下所示:
const puser = {
Name:'Test',
Age:'23',
Gender:'F',
}
axios.post('http://localhost:8000/AddUser', puser)
.then(res=> {
console.log(res.data)
})
如果问题仍然存在,请随时通知我
更新:
在邮递员中设置标题:
Axios.post还可以设置标题,如下所示:
axios.post('http://localhost:8000/AddUser',{ Name:'Test',
Age:'2311',
Gender:'FF'}, {headers: {
'content-type': 'application/json'
}})
.then(res=> {
console.log(res.data)
})
这是我的项目:
Program.cs:
using Demo_rest_ConsoleApp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp71
{
public class User
{
string name;
int age;
string gender;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
public string Gender
{
get
{
return gender;
}
set
{
gender = value;
}
}
}
[ServiceContract]
[CustContractBehavior]
public interface IService
{
[OperationContract]
[WebGet]
string EchoWithGet(string s);
[OperationContract]
[WebInvoke]
string EchoWithPost(string s);
[OperationContract]
[WebInvoke(ResponseFormat =WebMessageFormat.Json,RequestFormat =WebMessageFormat.Json)]
string AddUser(User U);
}
public class Service : IService
{
public string AddUser(User U)
{
Console.WriteLine(U.Age);
Console.WriteLine(U.Gender);
Console.WriteLine(U.Name);
return "success";
}
public string EchoWithGet(string s)
{
return "You said " + s;
}
public string EchoWithPost(string s)
{
return "You said " + s;
}
}
class Program
{
static void Main(string[] args)
{
WebServiceHost host = new WebServiceHost(typeof(Service), new Uri("http://localhost:8000/"));
ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "");
ep.EndpointBehaviors.Add(new WebHttpBehavior() { HelpEnabled=true});
host.Open();
Console.WriteLine("Open");
Console.ReadLine();
}
}
}
Soap.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Web;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml;
namespace Demo_rest_ConsoleApp
{
public class ServerMessageLogger : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
Console.WriteLine(request);
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
WebOperationContext ctx = WebOperationContext.Current;
ctx.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
if (ctx.IncomingRequest.Method == "OPTIONS")
{
ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
ctx.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "GET,POST,PUT,PATCH,POST,DELETE,OPTIONS");
ctx.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type");
}
}
}
public class ClientMessageLogger : IClientMessageInspector
{
public void AfterReceiveReply(ref Message reply, object correlationState)
{
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
return null;
}
}
[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class, AllowMultiple = false)]
public class CustContractBehaviorAttribute : Attribute, IContractBehavior, IContractBehaviorAttribute
{
public Type TargetContract => throw new NotImplementedException();
public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
return;
}
public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.ClientMessageInspectors.Add(new ClientMessageLogger());
}
public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
{
dispatchRuntime.MessageInspectors.Add(new ServerMessageLogger());
}
public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
{
return;
}
}
}
您使用了错误的参数位置<代码>车身数据应为第二个参数,
配置
应为第三个参数。这是axios.post签名axios.post(url[,data[,config]])
您的服务需要一个用户数组,但您发送了一个用户对象。尝试用数据切换标题,也尝试创建一个对象数组并传输它,但在后端服务中仍然没有收到数据。您好,问题解决了吗?如果您认为我的答复对您有帮助,您可以将其标记为答复。尝试过,但服务中仍未收到数据邮递员显示此错误。异常消息为“传入消息具有意外的消息格式”“Raw”。操作的预期消息格式为“Xml”、“Json”。我尝试了JSON.stringify,但它不起作用。不,它不起作用。我希望wcf没有任何变化,因为相同的服务正在使用angular应用程序。从react应用发送数据时一定有问题。我发布了我的项目,你可以参考它,它可以正常访问。