使用AJAX jQuery调用WCF端点的问题

时间:2011-06-26 13:16:01

标签: .net ajax wcf jquery jsonp

我有一个WCF 3.5服务,我需要使用jQuery作为跨域调用来调用它,当我调用服务时我已经返回正确的响应但jQuery抛出异常parsererror状态  这是我用来调用WCF服务的代码

<script type="text/javascript">
    $(document).ready(function () {
        var wordlist = $("#main-container").text()


        $.ajax({
            type: "GET",
            contentType: 'application/json; charset=utf-8',
            url: 'http://192.168.1.210:8080/XXXX.SPServices/GlossaryService.svc/GetWordsWithDifenition',
            dataType: 'jsonp',
            data: { 'stringwords': wordlist },
            success: function (data) {
                PutLinkToDefinition(data);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                debugger;
            }
        });
    });

</script>

jQuery调用错误函数并抛出此错误

  

jQuery161074459453323911_1309093997517   没被称为

这是我的WCF服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using Microsoft.SharePoint.Client.Application;
using Microsoft.SharePoint.Client;
using System.Net;
using System.Configuration;

namespace XXXX.SPServices
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

    public class GlossaryService
    {
        // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
        // To create an operation that returns XML,
        //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
        //     and include the following line in the operation body:
        //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
        [OperationContract]
        [WebGet]
        public Dictionary<string,string>  GetWordsWithDifenition(string stringwords)
        {
            ......
        }



}

这是标记

<%@ ServiceHost Language="C#" Debug="true" Service="XXXX.SPServices.GlossaryService" CodeBehind="GlossaryService.svc.cs"  Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

WCF 3.5不支持开箱即用的跨域调用/ JSONP。您必须升级到WCF 4或检查this sample