简体   繁体   English

如何从Ajax调用WCF服务?

[英]How to Call WCF service from ajax?

I have a WCF service as given below- 我有以下提供的WCF服务-

 [ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Sync
{
    [OperationContract]
    [WebInvoke]
    public string SyncDataNow(UserData obj)
    {
        try
        {
            using (MavenifyEntities db = new MavenifyEntities())
            {
                bool userExist = db.Users.Any(u => u.Id == obj.UserId);
                if (userExist)
                {
                    DataSync data = new DataSync();
                    data.UserId = obj.UserId;
                    data.TempId = obj.TempId;
                    data.Content = obj.Content;
                    data.CreatedDate = DateTime.Now.ToString();
                    db.DataSyncs.Add(data);
                    db.SaveChanges();
                    return "1";
                }
                else
                {
                    return "0";
                }
            }
        }
        catch (Exception ex)
        {
            return "error";
        }
    }
}

[DataContract(Namespace = "")]
public class UserData
{
    [DataMember]
    public int UserId { get; set; }
    [DataMember]
    public string Content { get; set; }
    [DataMember]
    public string TempId { get; set; }
}

When I am calling this service from POSTMAN, it receives null data, Please help me to find out what's wrong I am doing. 当我从POSTMAN调用此服务时,它接收到空数据,请帮助我找出我在做什么错。

I am sending raw data in json format like- 我正在以json格式发送原始数据,例如-

{"UserData":{"UserId":"1","TempId":"asdbsjiadf","Content":"Hello"}}. { “的UserData”:{ “用户ID”: “1”, “TempId”: “asdbsjiadf”, “内容”: “你好”}}。

my web.config entries are- 我的web.config条目是-

 <system.serviceModel>
<behaviors>
  <endpointBehaviors>
    <behavior name="PhoneSync.SyncAspNetAjaxBehavior">
      <enableWebScript />
    </behavior>
  </endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
  <service name="PhoneSync.Sync">
    <endpoint address="" behaviorConfiguration="PhoneSync.SyncAspNetAjaxBehavior" binding="webHttpBinding" contract="PhoneSync.Sync" /> 
  </service>
</services>

You should use Wcf Rest WebHttpBinding also You will face with Cross Domain Messaging issue. 您还应该使用Wcf Rest WebHttpBinding您将面临Cross Domain Messaging问题。 So You will disable cross messaging security or create own behavior for jsonp 因此,您将禁用跨消息传递安全性或为jsonp创建自己的行为

also your message should be {"UserId":"1","TempId":"asdbsjiadf","Content":"Hello"} for your signature. 并且您的消息应该是{"UserId":"1","TempId":"asdbsjiadf","Content":"Hello"}作为签名。 if you want to use {"UserData":{"UserId":"1","TempId":"asdbsjiadf","Content":"Hello"}} you should wrap it 如果要使用{"UserData":{"UserId":"1","TempId":"asdbsjiadf","Content":"Hello"}} ,则应将其包装起来

Make sure that request/response by default is Json . 确保默认情况下,请求/响应是Json I would specify that explicitly in [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json)] 我会在[WebInvoke(RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]中明确指定

And maybe make sense do configure some options like suggested here 也许有意义,请像此处建议的那样配置一些选项

I have resolved the problem by changing the name "UserData" to "obj" in my json ie changing 我已经通过在json中将名称“ UserData”更改为“ obj”解决了该问题,即更改了

{"UserData":{"UserId":"1","TempId":"asdbsjiadf","Content":"Hello"}} { “的UserData”:{ “用户ID”: “1”, “TempId”: “asdbsjiadf”, “内容”: “你好”}}

to

{"obj":{"UserId":"1","TempId":"asdbsjiadf","Content":"Hello"}} { “目标文件”:{ “用户ID”: “1”, “TempId”: “asdbsjiadf”, “内容”: “你好”}}

now I am receiving the data in my services. 现在我正在接收服务中的数据。 But I am unable to get why it is happening. 但是我不知道为什么会这样。 Please share the cause. 请分享原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM