Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JQuery Ajax调用WCF REST服务不起作用_Wcf - Fatal编程技术网

使用JQuery Ajax调用WCF REST服务不起作用

使用JQuery Ajax调用WCF REST服务不起作用,wcf,Wcf,我正在学习WCF服务。我试图从Jquery调用RESTful服务。我的服务如下 服务等级 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using System.ServiceModel.Activation; namespace RESTful

我正在学习WCF服务。我试图从Jquery调用RESTful服务。我的服务如下

服务等级

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;

namespace RESTfulServiceLib
{
[AspNetCompatibilityRequirements(RequirementsMode
    = AspNetCompatibilityRequirementsMode.Allowed)]
public class RestFullService : IRestFullService
{
    public string Welcome(string Name)
    {
        return  "Welcome to Restful Service " + Name;
    }
}
}
接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Activation;

namespace RESTfulServiceLib
{

[ServiceContract]

public interface IRestFullService
{
    [OperationContract]
    [WebInvoke(UriTemplate="/Welcome/{Name}",Method="GET",ResponseFormat=WebMessageFormat.Json)]
    string Welcome(string Name);
}
}
我已经创建了一个服务主机,svc文件如下所示

<%@ ServiceHost Language="C#" Debug="true" Service="RESTfulServiceLib.RestFullService" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
我尝试使用Jquery调用此服务。但是我越来越

error 200 undefined
剧本如下

<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.6.4.min.js"></script>
<script >
function ajaxcall()
{
    $.ajax({
        url: "http://localhost:2319/RESTFullService.svc/welcome/mahesh",
        type: "GET",
        contentType: "application/json; charset=utf-8",
        dataType:"jsonp",
        data:{},
        processdata : true,
        success: function(response)
        {
            var data= response.d;
            alert(data);
        },

        error: function(e)
        {
            alert('error '+e.status + ' ' + e.responseText);
        }
    });
}
$().ready(function(){
    $('#btntest').click(ajaxcall);
})  
</script>
</head>
<body>
<button id="btntest" >Click Me</button>
</body>
</html>

函数ajaxcall()
{
$.ajax({
url:“http://localhost:2319/RESTFullService.svc/welcome/mahesh",
键入:“获取”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“jsonp”,
数据:{},
processdata:对,
成功:功能(响应)
{
var数据=响应d;
警报(数据);
},
错误:函数(e)
{
警报('error'+e.status+''+e.responseText);
}
});
}
$().ready(函数()){
$('btntest')。单击(ajaxcall);
})  
点击我
我的编码有什么问题?请帮帮我

谢谢


Mahesh

在成功使用jQuery的过程中

// var data= response.d;
但是,您没有返回有效的json。
您的回复中没有“d”。

这是由于跨域问题造成的。我在配置文件中做了一些更改,效果很好

更改后的配置文件。使用crossDomainScriptAccessEnabled=“true”添加了新的绑定配置并添加到端点。并将aspNetCompatibilityEnabled=“false”


修改脚本后,我在URL末尾添加了“?callback=?”,以获取JSON而不是JSONP格式的输出。JSONP将在URL中不提供回调的情况下工作

<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.6.4.min.js"></script>
<script type="text/javascript" language="javascript">
    function ajaxcall()
    {
        $.ajax({
            url: "http://localhost:2319/RESTFullService.svc/welcome/mahesh?calback=?",
            type: "GET",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: {},
            processdata: true,
            success: function (response) {
                var data = response;
                alert(data);
            },

            error: function (e) {
                alert('error ' + e.status + ' ' + e.responseText);

            }
            });
    }

    $().ready(function(){
        $('#btntest').click(ajaxcall);
    })  
</script>
</head>
<body>
    <button id="btntest" >Click Me</button>
</body>
</html>

函数ajaxcall()
{
$.ajax({
url:“http://localhost:2319/RESTFullService.svc/welcome/mahesh?calback=?",
键入:“获取”,
数据类型:“json”,
contentType:“应用程序/json;字符集=utf-8”,
数据:{},
processdata:对,
成功:功能(响应){
var数据=响应;
警报(数据);
},
错误:函数(e){
警报('error'+e.status+''+e.responseText);
}
});
}
$().ready(函数()){
$('btntest')。单击(ajaxcall);
})  
点击我

感谢我在Calsoft实验室的同事普罗尼夫人帮助我解决了这个问题

谢谢您的回复。我在成功部分尝试了“警惕(“成功”)”。但我也犯了同样的错误。我想,由于其他一些错误,服务没有成功,我添加了“alert(XMLHttpRequest.responseText);”和“警报(如状态文本);”此外,错误部分现在和尝试。现在e.statusText显示“Success”。这意味着什么?
function CallService(sucessData) {
$.ajax({
    // Add code for Cross Domain
    headers: getHeaders(),
    type: varType, //GET or POST or PUT or DELETE verb
    url: varUrl, // Location of the service
    data: varData, //Data sent to server
    contentType: varContentType, // content type sent to server
    dataType: varDataType, //Expected data format from server
    processdata: varProcessData, //True or False
    crossDomain: true,
    cache: varCache,
    timeout: 200000,
    success: sucessData,
    error: function (xhr) {// When Service call fails
        fancyAlert("Error: " + xhr.responseText);
        //fancyAlert('Error occured in Service Call');
    }
});
}
<configuration>

<system.web>
    <compilation debug="true" targetFramework="4.0" />
</system.web>

<system.serviceModel>
    <standardEndpoints />
    <bindings>
        <webHttpBinding>
            <binding name="Bind1" crossDomainScriptAccessEnabled="true" />
        </webHttpBinding>
    </bindings>
    <behaviors>
        <endpointBehaviors>
            <behavior name="EndPBhvr">
                <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"
                    faultExceptionEnabled="true" />
            </behavior>
        </endpointBehaviors>
        <serviceBehaviors>
            <behavior name="SvcBhvr">
                <serviceMetadata httpGetEnabled="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
       <serviceHostingEnvironment aspNetCompatibilityEnabled="false" />
    <services>
        <service behaviorConfiguration="SvcBhvr" name="RESTfulServiceLib.RestFullService">
            <endpoint address="" behaviorConfiguration="EndPBhvr" binding="webHttpBinding"
                bindingConfiguration="Bind1" name="EP1" contract="RESTfulServiceLib.IRestFullService" />
        </service>
    </services>
</system.serviceModel>
</configuration>
<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.6.4.min.js"></script>
<script type="text/javascript" language="javascript">
    function ajaxcall()
    {
        $.ajax({
            url: "http://localhost:2319/RESTFullService.svc/welcome/mahesh?calback=?",
            type: "GET",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: {},
            processdata: true,
            success: function (response) {
                var data = response;
                alert(data);
            },

            error: function (e) {
                alert('error ' + e.status + ' ' + e.responseText);

            }
            });
    }

    $().ready(function(){
        $('#btntest').click(ajaxcall);
    })  
</script>
</head>
<body>
    <button id="btntest" >Click Me</button>
</body>
</html>
function CallService(sucessData) {
$.ajax({
    // Add code for Cross Domain
    headers: getHeaders(),
    type: varType, //GET or POST or PUT or DELETE verb
    url: varUrl, // Location of the service
    data: varData, //Data sent to server
    contentType: varContentType, // content type sent to server
    dataType: varDataType, //Expected data format from server
    processdata: varProcessData, //True or False
    crossDomain: true,
    cache: varCache,
    timeout: 200000,
    success: sucessData,
    error: function (xhr) {// When Service call fails
        fancyAlert("Error: " + xhr.responseText);
        //fancyAlert('Error occured in Service Call');
    }
});
}