403制作' ajax'打电话给网络API

时间:2015-01-05 14:16:58

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

我正在调用我的后端来填充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);
    }

2 个答案:

答案 0 :(得分:2)

现在你的后端方法只返回" 200 - OK"如果您选择的页面没有内容。否则,如果页面包含某些内容,则始终返回403.

我觉得你可能无意中改变了你的三元运营商?

也许试试:

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

答案 1 :(得分:0)

更改

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

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