【问题标题】:403 Response when making 'ajax' call to web api对 web api 进行“ajax”调用时出现 403 响应
【发布时间】:2015-03-03 02:25:38
【问题描述】:

我正在调用我的后端来填充 observableArray,如下所示:

$.ajax({
    url: 'www.data.net/api/....',
    type: 'GET',
    dataType: 'jsonp',
    crossDomain: true,
    success: function (data) {
        dataHolder(data);
    },
    error: function (xhr, textStatus, errorThrown) {
        console.log(errorThrown);
    }
});

后端方法:

    [Route("Api/mydata/GetMyContent/{postPageName}"), HttpGet]
           public HttpResponseMessage GetMyContent(string postPageName)
            {
                var result = mydataRepository.GetMyContent(postPageName);
                return Request.CreateResponse(result == null ? HttpStatusCode.OK :             
                       HttpStatusCode.Forbidden, result);
    }

【问题讨论】:

  • 这可能是后端问题。你能把你正在调用的webapi方法装起来吗?
  • @WayneEllery Ellery 我已经更新了问题

标签: ajax knockout.js asp.net-web-api jsonp fiddler


【解决方案1】:

改变

result == null ? HttpStatusCode.OK : HttpStatusCode.Forbidden

result != null ? HttpStatusCode.OK : HttpStatusCode.Forbidden

【讨论】:

    【解决方案2】:

    现在,如果您选择的页面没有内容,您的后端方法只会返回“200 - OK”。否则,如果页面有一些内容,你总是返回 403。

    我觉得你可能只是无意中改变了你的三元运算符?

    不妨试试:

        return Request.CreateResponse(result == null ? HttpStatusCode.Forbidden:
                                      HttpStatusCode.OK, result);
    

    【讨论】:

      猜你喜欢
      • 2014-06-25
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      相关资源
      最近更新 更多