简体   繁体   English

Ajax到WCF,无法将Ajax加载到对象中

[英]Ajax to WCF, can't load Ajax into an object

I want to send some data back form my JavaScript to WCF. 我想将一些数据从我的JavaScript发送回WCF。 This object I send back needs to get loaded in the Foo class. 我发回的对象需要加载到Foo类中。 If I debug the code I can see the function (Sting) gets called. 如果调试代码,我可以看到该函数(Sting)被调用。 But if i check whats in the the object I got returned, this object is null. 但是,如果我检查返回的对象中的内容,则此对象为null。

This indicates the data can't be stored in in the Object of WCF. 这表明数据无法存储在WCF的对象中。 The WCF works find when I send data to the JavaScript with Ajax. 当我使用Ajax将数据发送到JavaScript时,WCF会进行查找。 FYI I use .NET 3.5 仅供参考我使用.NET 3.5

this is how I try to receive the data: WCF: 这是我尝试接收数据的方式:WCF:

namespace TPlatform
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class DataService
    {
        [OperationContract]
        public void Sting(Foo postData)
        {
            var x = 1; //Breakpoint, postData is null?
        }
    }

    [DataContract]
    public class Foo
    {
        [DataMember]
        public string Bar;
    }
}

JavaScript: JavaScript的:

var fooObject = { "Bar": "test" };
function sendDataToWcf(object) {
    $.ajax({
        type: "POST",
        url: "DataService.svc/Sting",
        data: JSON.stringify(fooObject),
        processData: false,
        contentType: "application/json",
        dataType: "json",
        success: suckcess,
        error: showError
    });
}

What am I doing wrong? 我究竟做错了什么? How can I read the the data into my class? 如何将数据读入班级?

Try this 尝试这个

        $.ajax({
            type: 'POST',
            url: url,
            //dataType: 'json',
            contentType: "application/json",
            data: JSON.stringify({ postData: fooObject}),
            success: function (response) {
                successCallback(response);
            },
            error: function (xhr, status, error) {
                handleError(xhr, status, error);
            }
        });

Update: Change: 更新:更改:

var fooObject = { "Bar": "test" };

too

var fooObject = { Bar: "test" };

and dont send the datatype. 并且不发送数据类型。

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

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