2012-04-11 82 views
0

我试图做一个跨域POST到WCF休息服务。以下是我的服务代码,获取请求jQuery ajax跨域发布到WCF休息服务

[WebInvoke(UriTemplate = "", Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
    public SampleItem Create(SampleItem instance) 
    { 

     instance.StringValue += " -success"; 
     return instance; 
    } 

而在客户端,我有以下的jquery ajax调用

var input = { "Id": 10, "StringValue": "Test Value" }; 

       $("#post").click(function() { 
        $.ajax({ 
         type: "POST", 
         url: "http://localhost:50577/Service1/", 
         dataType: "jsonp", 
         data: JSON.stringify(input), 
         processData: false, 
         success: function(item) { 
          $("#itemId").val(item.Id); 
          $("#name").val(item.StringValue); 
         }, 
         error: function(xhr) { 
          alert("error " + xhr.responseText); 
         } 
        }); 

当调试服务器端代码的请求来的方法但实例参数为null。在同一个项目中,我有一个跨域的get,它没有任何问题。 如果我将客户端更改为在同一个域中,并将数据类型更改为json,它可以正常工作。

那么跨域的帖子是不可能的?或者还有什么我需要做的才能做到这一点。

回答

0

您解答了您的第一个问题,因为您可以调试该服务!

AJAX调用的JSON输入格式不正确。试试这个:

... 
data: JSON.stringify({instance : input}), 
...