在Asp.Net中的Jquery中调用Wcf服务的问题 [英] Problem in Calling Wcf Service In Jquery in Asp.Net

查看:63
本文介绍了在Asp.Net中的Jquery中调用Wcf服务的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//当我在Jquery中运行My Form然后服务失败函数时!..

但我不知道问题出在哪里?

< pre lang =HTML> // WebForm中调用WCF服务的代码
< html xmlns = http://www.w3.org/1999/xhtml >
< head runat = server >
< title > < / title >
< script < span class =code-attribute> type = text / javascript src = http://code.jquery.com/jquery-1.8.2.js > ; < / script >
< script type = text / javascript >
var 类型;
var 网址;
var 数据;
var ContentType;
var DataType;
function CallService(){
$ .ajax({
type:Type, // GET或POST或PUT或DELETE动词
url:Url, // 服务的位置
数据:数据, // 发送到服务器的数据
contentType:ContentType, // 发送到的内容类型服务器
dataType:DataType, // 来自服务器的预期数据格式
成功: function (msg){ // On成功的服务电话
ServiceSucceeded(MSG);
},
错误:ServiceFailed // 服务调用失败时
});
}
function ServiceFailed(result){
alert(' 服务调用失败:' + result.status + ' ' + result.statusText);
Type = null ; Url = null ;数据= null ; ContentType = null ; DataType = null ; ProcessData = null ;
}
function WCFJSON(){
var id = < span class =code-string> 1;
Type = GET;
Url = http:// localhost:52410 / RestServiceImpl.svc / JSONData;
数据= ' {id=' + id + ' }';
// 数据= {};
ContentType = application / json; charset = utf-8;
DataType = json;
CallService();
}
function ServiceSucceeded(result){

if (DataType == json){
resultObject = result.JSONDataResult;
for (i = 0 ; i< resultObject.length; i ++){
alert(resultObject [i]);
}
}
}
$( document )。ready(
function (){
WCFJSON();
}
);

< / script >
// Web配置中的代码
< system.serviceModel >
< 服务 >
< service name = RestService.RestServiceImpl behaviorConfiguration = ServiceBehaviour >
<! - 服务端点 - >
<! - 除非完全限定,否则地址相对于上面提供的基地址 - >
< endpoint 地址 = binding = webHttpBinding 合同 = RestS ervice.IRestServiceImpl behaviorConfiguration = web >
< ; / endpoint >
< / service >
< / services >

< span class =code-keyword>< 行为 >
< serviceBehaviors >
< 行为 名称 = ServiceBehaviour >
<! - 为避免泄露元数据信息,请将以下值设置为false,并在部署之前删除上面的元数据端点 - >
< serviceMetadata httpGetEnabled = true / >
<! - 收到例外情况出于调试目的,在故障中详细说明,将下面的值设置为true。在部署之前设置为false以避免泄露异常信息 - >
< serviceDebug includeExceptionDetailInFaults = false / >
< / behavior < span class =code-keyword>>
< / serviceBehaviors >
< endpointBehaviors >
< b行为 名称 = web >
< webHttp / >
< / behavior >
< / endpointBehaviors >
< / behavior >
< serviceHostingEnvironment multipleSiteBindingsEnabled = true / >
< /system.serviceModel>
< system.webServer >
< modules runAllManagedModulesForAllRequests = true / >
< / system.webServer >

< / configuration >

//代码在Wcf服务中......
//我的InterFace IRestServiceImpl In Wcf Service!...
[OperationContract]
[WebInvoke(Method =GET,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped)
]
string XMLData(string id);

[OperationContract]
[WebInvoke(Method =GET,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)
]
string JSONData(string id);
}
}

//这是InterFace实现
公共类RestServiceImpl:IRestServiceImpl
{
public string XMLData(string id)
{
返回您要求的产品+ id;
}
public string JSONData(string id)
{
returnYou requested product+ id;
}

解决方案

.ajax({
type:Type, // GET或POST或PUT或DELETE动词
url:Url, // 服务的位置
数据:数据, // 发送到服务器的数据
contentType:ContentType, // 发送的内容类型到服务器
dataType:DataType, // 来自服务器的预期数据格式
成功: function (msg){ // 在成功的服务电话上
ServiceSucceeded(msg);
},
错误:ServiceFailed // 服务调用失败时
});
}
function ServiceFailed(result){
alert(' 服务调用失败:' + result.status + ' ' + result.statusText);
Type = null ; Url = null ;数据= null ; ContentType = null ; DataType = null ; ProcessData = null ;
}
function WCFJSON(){
var id = < span class =code-string> 1;
Type = GET;
Url = http:// localhost:52410 / RestServiceImpl.svc / JSONData;
数据= ' {id=' + id + ' }';
// 数据= {};
ContentType = application / json; charset = utf-8;
DataType = json;
CallService();
}
function ServiceSucceeded(result){

if (DataType == json){
resultObject = result.JSONDataResult;
for (i = 0 ; i< resultObject.length; i ++){
alert(resultObject [i]);
}
}
}


文档)。ready(
function (){
WCFJSON();
}
);

< / script >
// Web配置中的代码
< system.serviceModel >
< 服务 >
< service name = RestService.RestServiceImpl behaviorConfiguration = ServiceBehaviour >
<! - 服务端点 - >
<! - 除非完全限定,否则地址相对于上面提供的基地址 - >
< endpoint 地址 = binding = webHttpBinding 合同 = RestS ervice.IRestServiceImpl behaviorConfiguration = web >
< ; / endpoint >
< / service >
< / services >

< span class =code-keyword>< 行为 >
< serviceBehaviors >
< 行为 名称 = ServiceBehaviour >
<! - 为避免泄露元数据信息,请将以下值设置为false,并在部署之前删除上面的元数据端点 - >
< serviceMetadata httpGetEnabled = true / >
<! - 收到例外情况出于调试目的,在故障中详细说明,将下面的值设置为true。在部署之前设置为false以避免泄露异常信息 - >
< serviceDebug includeExceptionDetailInFaults = false / >
< / behavior < span class =code-keyword>>
< / serviceBehaviors >
< endpointBehaviors >
< b行为 名称 = web >
< webHttp / >
< / behavior >
< / endpointBehaviors >
< / behavior >
< serviceHostingEnvironment multipleSiteBindingsEnabled = true / >
< /system.serviceModel>
< system.webServer >
< modules runAllManagedModulesForAllRequests = true / >
< / system.webServer >

< / configuration >

//代码在Wcf服务中......
//我的InterFace IRestServiceImpl In Wcf Service!...
[OperationContract]
[WebInvoke(Method =GET,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped)
]
string XMLData(string id);

[OperationContract]
[WebInvoke(Method =GET,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)
]
string JSONData(string id);
}
}

//这是InterFace实现
公共类RestServiceImpl:IRestServiceImpl
{
public string XMLData(string id)
{
返回您要求的产品+ id;
}
public string JSONData(string id)
{
returnYou requested product+ id;
}


我可以在代码中看到一个问题。

这是缺少起始脚本标记。

您的整个JavaScript代码应该在脚本标记内,但是没有脚本的开始标记。



就在代码<$ c $之上c> var Type; ,你应该有一个脚本标签,如< script> 。您有一个结束标记,但没有开始标记。

请加上这个。



谢谢......


//When I run My Form Then Service Failed Function In Jquery Called!..
But I don''t know where is the problem??

//Code For Call WCF Service In WebForm
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>  
<script type="text/javascript"> 
    var Type;
        var Url;
        var Data;
        var ContentType;
        var DataType;               
        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             
                success: function (msg) {//On Successfull service call
                    ServiceSucceeded(msg);
                },
                error: ServiceFailed// When Service call fails
            });
        }
        function ServiceFailed(result) {
            alert('Service call failed: ' + result.status + '' + result.statusText);
            Type = null; Url = null; Data = null; ContentType = null; DataType = null; ProcessData = null;
        }
        function WCFJSON() {
            var id = "1";
            Type = "GET";
            Url = "http://localhost:52410/RestServiceImpl.svc/JSONData";
           Data = '{"id"= "' + id + '"}';
           // Data = {};
            ContentType = "application/json; charset=utf-8";
            DataType = "json"; 
            CallService();
        }
        function ServiceSucceeded(result) {

            if (DataType == "json") {
                resultObject = result.JSONDataResult;
                for (i = 0; i < resultObject.length; i++) {
                    alert(resultObject[i]);
                }
            }
        }      
        $(document).ready(
         function () {
             WCFJSON();
         }
         );        
        
    </script>   
//Code In Web Config
  <system.serviceModel>
    <services>
      <service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address ="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
        </endpoint>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

//Code In Wcf Service...
//My InterFace IRestServiceImpl In Wcf Service!...
        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Wrapped)
            ]
        string XMLData(string id);

        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped)
           ]
        string JSONData(string id);
    }
}

//This Is InterFace Implementaion
    public class RestServiceImpl : IRestServiceImpl
    {      
        public string XMLData(string id)
        {
            return "You requested product " + id;
        }
        public string JSONData(string id)
        {
            return "You requested product " + id;
        }

解决方案

.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 success: function (msg) {//On Successfull service call ServiceSucceeded(msg); }, error: ServiceFailed// When Service call fails }); } function ServiceFailed(result) { alert('Service call failed: ' + result.status + '' + result.statusText); Type = null; Url = null; Data = null; ContentType = null; DataType = null; ProcessData = null; } function WCFJSON() { var id = "1"; Type = "GET"; Url = "http://localhost:52410/RestServiceImpl.svc/JSONData"; Data = '{"id"= "' + id + '"}'; // Data = {}; ContentType = "application/json; charset=utf-8"; DataType = "json"; CallService(); } function ServiceSucceeded(result) { if (DataType == "json") { resultObject = result.JSONDataResult; for (i = 0; i < resultObject.length; i++) { alert(resultObject[i]); } } }


(document).ready( function () { WCFJSON(); } ); </script> //Code In Web Config <system.serviceModel> <services> <service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour"> <!-- Service Endpoints --> <!-- Unless fully qualified, address is relative to base address supplied above --> <endpoint address ="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web"> </endpoint> </service> </services> <behaviors> <serviceBehaviors> <behavior name="ServiceBehaviour"> <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> <serviceMetadata httpGetEnabled="true"/> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="web"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration> //Code In Wcf Service... //My InterFace IRestServiceImpl In Wcf Service!... [OperationContract] [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped) ] string XMLData(string id); [OperationContract] [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped) ] string JSONData(string id); } } //This Is InterFace Implementaion public class RestServiceImpl : IRestServiceImpl { public string XMLData(string id) { return "You requested product " + id; } public string JSONData(string id) { return "You requested product " + id; }


I can see one problem in the code.
That is the starting script tag is missing.
Your whole JavaScript Code should be inside script tags, but there is no start tag of script.

Just above the code var Type;, you should have one script tag like <script>. You have one end tag for this, but no start tag.
Please add this.

Thanks...


这篇关于在Asp.Net中的Jquery中调用Wcf服务的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆