【问题标题】:WCF as a Data Service for html pages. jQuery ajax function.WCF 作为 html 页面的数据服务。 jQuery ajax 函数。
【发布时间】:2017-07-07 23:43:45
【问题描述】:

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

WCF DataService.svc

公共函数 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:

// 访问 WCF Rest 服务方法的 URL 变量 urlsource ; urlsource = "http://localhost/ehealthservice/DataService.svc/"; 变种变种; var varUrl; 变种方法; 变量变量数据; 变量变量内容类型; var varDataType; var varProcessData; //调用AXMX/WCF服务的通用函数 函数调用服务C(){ 调试器; var categoryid = getQueryStringValue('CategoryId'); var oData; 调试器; $.ajax({ 缓存:假, type: varType, //GET 或 POST 或 PUT 或 DELETE 动词 url: varUrl, // 服务的位置 data: oData, //发送到服务器的数据 contentType: varContentType, // 发送到服务器的内容类型 跨域:是的, 数据类型:varDataType, 超时:2000, success: function (msg) {//成功的服务调用 调试器; // 我想到达这里 返回html; } }, error: ServiceFailedC// 当服务调用失败时 }); } 功能ServiceFailedC(结果){ 调试器; var myJSON = JSON.stringify(result); alert('服务调用失败:' + result.status + '' + result.statusText); 变量类型 = 空; varUrl = null; varData = null; varContentType = null; varDataType = null; varProcessData = null; } 函数 GetHealthcareByCategory() { 调试器; varType = "POST"; varUrl = urlsource + "GetHealthcareByCategoryId/1"; varContentType = "应用程序/json; charset=utf-8"; varDataType = "jsonp"; varProcessData = 假; 方法 = "GetHealthcare"; 呼叫服务C(); } 函数getQueryStringValue(键){ 调试器; return unescape(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + escape(key).replace(/[\.\+\*]/g, "\ \$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1")); }; $(文档).ready( 功能 () { GetHealthcareByCategory(); } );

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

【问题讨论】:

    标签: post put


    【解决方案1】:

    经过几天的搜索和尝试,终于搞定了。必须通过将其添加到 Global.asax 的 begin_Request 中来启用跨域资源共享 (CORS) 并清理 Web 配置。解决方案的链接在这里Enable CORS In WCF

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-12
      • 2023-03-11
      相关资源
      最近更新 更多