WCF 作为 html 页面的数据服务。 jQuery ajax 函数。

WCF as a Data Service for html pages. jQuery ajax function.

get 工作正常,但使用 POST 或 PUT 按 id 获取返回错误 404。

WCF DataService.svc

Public 函数 GetHealthcare(ByVal CategoryId As String) 作为 vwHealthcare 实现 IDataService.GetHealthcare 将 nSubject 调暗为新 vwHealthcare

    Dim context As DataClasses1DataContext = New DataClasses1DataContext()
    'Check if exists
    Dim res = From b In context.vwHealthcares Where b.CategoryId = CategoryId
              Select b
    Dim uList As List(Of vwHealthcare) = res.ToList()

    Return res
End Function

IDataService

<WebInvoke(Method:="POST", ResponseFormat:=WebMessageFormat.Json, UriTemplate:="GetHealthcareByCategoryId/{CategoryId}")>
<OperationContract()>
Function GetHealthcare(ByVal CategoryId As String) As vwHealthcare

我的客户Html:





        // Urls to access the WCF Rest service methods


        var urlsource ;

       urlsource = "http://localhost/ehealthservice/DataService.svc/";
        var varType;
        var varUrl;
        var method;
        var varData;
        var varContentType;
        var varDataType;
        var varProcessData;

        //Generic function to call AXMX/WCF  Service
        function CallServiceC() {
            debugger;
            var categoryid = getQueryStringValue('CategoryId');

            var oData;

            debugger;
            $.ajax({
                cache: false,
                type: varType, //GET or POST or PUT or DELETE verb
                url: varUrl, // Location of the service
                data: oData, //Data sent to server

                contentType: varContentType, // content type sent to server
                crossDomain: true,
                dataType: varDataType,
                timeout: 2000,

                success: function (msg) {//On Successfull service call

                    debugger;
                  // I want to get here





                            return html;

                        }

                },
                error: ServiceFailedC// When Service call fails
            });


        }


        function ServiceFailedC(result) {
            debugger;
            var myJSON = JSON.stringify(result);

            alert('Service call failed: ' + result.status + '' + result.statusText);
            varType = null; varUrl = null; varData = null; varContentType = null; varDataType = null; varProcessData = null;
        }


        function GetHealthcareByCategory() {
            debugger;

           varType = "POST";
           varUrl = urlsource + "GetHealthcareByCategoryId/1";

            varContentType = "application/json; charset=utf-8";
            varDataType = "jsonp";
            varProcessData = false;
            method = "GetHealthcare";

            CallServiceC();
        }

        function getQueryStringValue(key) {
            debugger;
            return unescape(window.location.search.replace(new RegExp("^(?:.*[&\?]" + escape(key).replace(/[\.\+\*]/g, "\$&") + "(?:\=([^&]*))?)?.*$", "i"), ""));
        };
        $(document).ready(

    function () {




        GetHealthcareByCategory();
    }

    );
    

注: 我尝试了很多方法仍然是相同的 404 错误。我也在文件夹上启用了 read/write 权限。此示例遵循 link enter link description here 的 Pranya 示例,我仍然收到错误 404,请家里的任何开发人员。

经过几天的搜索和尝试,终于弄明白了。必须通过在 Global.asax 的 begin_Request 中添加它来启用 Cross-Origin 资源共享 (CORS) 并清理 Web 配置。解决方案 link 在这里 Enable CORS In WCF

注意:我通过使用 jsonp 在 Get 中逃脱了它,但是 POST , PUT 和 DELETE 无法进行。 非常感谢。