2011-11-27 116 views
3

我想使用JSONP调用WCF服务。有两种方法, 1.“你好”的方法,它不需要任何参数。这工作正常 2.“Hello1”方法需要一个字符串参数。这不能正常工作。它应该返回我传递的价值。无法使用JSONP将参数传递给REST/WCF服务

以下是代码片段

Server代码

using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Activation; 
using System.ServiceModel.Web; 

namespace WcfService1 
{ 

    [ServiceContract(Namespace = "JsonpAjaxService")] 
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
    public class Service1 
    { 

     [WebGet(ResponseFormat = WebMessageFormat.Json)] 
     public string Hello() 
     { 
      return "I got the call"; 
     } 

     [WebGet(ResponseFormat = WebMessageFormat.Json)] 
     public string Hello1(string str) 
     { 
      return "Hi, you passed me " + str; 
     } 

    } 

} 

Web配置代码

<?xml version="1.0"?> 
<configuration> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    <authentication mode="None" /> 
    </system.web> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 
    <system.serviceModel> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 
    <standardEndpoints> 
     <webScriptEndpoint> 
     <standardEndpoint name="" crossDomainScriptAccessEnabled="true"/> 
     </webScriptEndpoint> 
     <webHttpEndpoint> 
     <standardEndpoint name="" helpEnabled="True" 
      automaticFormatSelectionEnabled="true" /> 
     </webHttpEndpoint> 
    </standardEndpoints> 
    </system.serviceModel> 
</configuration> 

客户端代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <script src="JS/JQuery.js" type="text/javascript"></script> 
    <script type="text/javascript"> 


     var Type; 
     var Url; 
     var Data; 
     var ContentType; 
     var DataType; 
     var ProcessData; 
     var method; 
     //Generic function to call WCF Service 
     function CallService() { 
      $.ajax({ 
       type: Type, //GET or POST or PUT or DELETE verb 
       url: Url, // Location of the service 
       data: Data, //Data sent to server 
       contentType: ContentType, // content type sent to server 
       dataType: DataType, //Expected data format from server 
       processdata: ProcessData, //True or False 
       success: function (msg) {//On Successfull service call 
        ServiceSucceeded(msg); 
       }, 
       error: ServiceFailed// When Service call fails 
      }); 
     } 

     function ServiceFailed(result) { 
      alert('test'); 
      alert('Service call failed: ' + result.status + '' + result.statusText); 
      Type = null; 
      Url = null; 
      Data = null; 
      ContentType = null; 
      DataType = null; 
      ProcessData = null; 
     } 

     function Hello() { 
      var uesrid = "1"; 
      Type = "GET"; 
      Url = "http://localhost:52136/Service1.svc/Hello"; 
      DataType = "jsonp"; ProcessData = false; 
      method = "Hello"; 
      //debugger; 
      CallService(); 
     } 

     function Hello1() { 
      debugger; 
      var uesrid = "1"; 
      Type = "GET"; 
      ContentType = "application/json; charset=utf-8"; 
      Url = "http://localhost:52136/Service1.svc/Hello1"; 
      DataType = "jsonp"; //ProcessData = false; 
      method = "Hello1"; 
      Data = "{'str':'abc'}"; //"{'Input':'a123'}";//"{'StringValue':'1234'}"; 
      //debugger; 
      CallService(); 
     } 


     function ServiceSucceeded(result) { 
      debugger; 
      if (DataType == "jsonp") { 
       alert(result); /* Problem, While calling 'Hello1' method I do not get the value in the return string*/ 
      } 
     } 

     function ServiceFailed(xhr) { 
      alert(xhr.responseText); 
      if (xhr.responseText) { 
       var err = xhr.responseText; 
       if (err) 
        error(err); 
       else 
        error({ Message: "Unknown server error." }) 
      } 
      return; 
     } 

     $(document).ready(
     function() { 
      try { 
       //Hello(); 
       Hello1(); 
      } catch (exception) { } 
     } 
); 

    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <h1> 
     JSONP Service Client Page</h1> 
    </form> 
</body> 
</html> 

谁能告诉我什么是错在日同时呼吁Hello1方法

阿图尔Sureka

回答

3

尝试传送数据为对象,而不是作为一个串E代码:

function Hello1() { 
    debugger; 
    var uesrid = "1"; 
    Type = "GET"; 
    ContentType = "application/json; charset=utf-8"; 
    Url = "http://localhost:52136/Service1.svc/Hello1"; 
    DataType = "jsonp"; //ProcessData = false; 
    method = "Hello1"; 
    Data = {str : 'abc'}; 
    //debugger; 
    CallService(); 
} 
+0

非常感谢,它像一个魅力。凉 –

1
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Activation; 
using System.ServiceModel.Web; 

namespace WcfService1 
{ 

    [ServiceContract(Namespace = "JsonpAjaxService")] 
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
    public class Service1 
    { 

     [WebGet(ResponseFormat = WebMessageFormat.Json)] 
     public string Hello() 
     { 
      return "I got the call"; 
     } 

     [WebGet(BodyStyle = WebMessageBodyStyle.Bare, 
     ResponseFormat = WebMessageFormat.Json, 
     UriTemplate = "SetQuoteEntities?str={str}")] 
     public string Hello1(string str) 
     { 
      return "Hi, you passed me " + str; 
     } 

    } 

} 

当我修改的属性webGet它为我工作。