Post WCF作为html页面的数据服务。jqueryajax函数。

Post WCF作为html页面的数据服务。jqueryajax函数。,post,put,Post,Put,获取工作正常,但使用POST或PUT的获取id返回错误404。 WCF DataService.svc 作为vwHealthcare实现IDataService.GetHealthcare的公共函数GetHealthcare(ByVal CategoryId为字符串) Dim NSObject作为新的vwHealthcare Dim context As DataClasses1DataContext = New DataClasses1DataContext() 'Check

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

WCF DataService.svc

作为vwHealthcare实现IDataService.GetHealthcare的公共函数GetHealthcare(ByVal CategoryId为字符串) Dim NSObject作为新的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

函数GetHealthcare(ByVal CategoryId作为字符串)作为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"), "$1")); }; $(document).ready( function () { GetHealthcareByCategory(); } ); //访问WCF Rest服务方法的URL 变量源; urlsource=”http://localhost/ehealthservice/DataService.svc/"; 变量类型; var-varUrl; var方法; var-varData; var-varContentType; var-varDataType; var-varProcessData; //调用AXMX/WCF服务的通用函数 函数CallServiceC(){ 调试器; var categoryid=getQueryStringValue('categoryid'); 小田变种; 调试器; $.ajax({ cache:false, type:varType,//GET或POST或PUT或DELETE动词 url:varUrl,//服务的位置 data:oData,//发送到服务器的数据 contentType:varContentType,//内容类型已发送到服务器 跨域:是的, 数据类型:varDataType, 超时时间:2000, success:function(msg){//On Successfull service call 调试器; //我想到这里 返回html; } }, 错误:ServiceFailedC//当服务调用失败时 }); } 功能服务失败(结果){ 调试器; var myJSON=JSON.stringify(结果); 警报('服务调用失败:'+result.status+'+result.statusText); varType=null;varUrl=null;varData=null;varContentType=null;varDataType=null;varProcessData=null; } 函数GetHealthcareByCategory(){ 调试器; varType=“POST”; varUrl=urlsource+“GetHealthcareByCategoryId/1”; varContentType=“application/json;charset=utf-8”; varDataType=“jsonp”; varProcessData=false; 方法=“GetHealthcare”; CallServiceC(); } 函数getQueryStringValue(键){ 调试器; 返回unescape(window.location.search.replace(新的RegExp(“^(?:.[&\\?]”加escape(键)。replace(/[\.\+\*]/g,\\$&”)+”(?:\\=([^&]*)?)?*,“i”),“$1”); }; $(文件)。准备好了吗( 函数(){ GetHealthcareByCategory(); } ); 注意:
我已经尝试了很多方法,但仍然是相同的404错误。我还启用了文件夹的读/写权限。此示例使用链接跟踪Pranya示例,但我仍然收到404错误,请所有开发人员注意。

经过几天的搜索和尝试,终于找到了答案。必须通过在Global.asax的begin_请求中添加跨源资源共享(CORS)并清理web配置来启用跨源资源共享(CORS)。这里是解决方案的链接

注意:我通过使用jsonp在Get中侥幸逃脱,但是POST、PUT和DELETE无法继续。 非常感谢