0

我正在尝试使用 ajax 进行 API 调用:

svc.authenticateAdmin = function (id, code) {
    $.ajax({
        url: 'api/event/authenticate',
        data: { 'id': id, 'code': code },
        datatype: 'json',
        contentType: 'application/json',
        type: 'GET',
        success: function (data) {
            App.eventBus.publish('authenticationComplete', data);
        }
    });
};

API Controller 中的方法:

[ActionName("All")]
public bool Authenticate(int id, string code)
{
    var repo = new MongoRepository<Event>(_connectionString);
    var entry = repo.FirstOrDefault(e => e.Id == id);
    return entry.AdminPassword == code;
}

但我收到 404 错误:urlstuff/api/event/authenticate?id=123&code=abc 404 (Not Found)

我已经从许多已知的工作调用(我没有写)中复制了实现。看起来像:

svc.getEventFromCode = function (code) {
    $.ajax({
        url: '/api/event/',
        data: { 'code': code },
        dataType: 'json',
        type: 'GET',
        success: function (data) {
            App.eventBus.publish('loadedEvent', data);
            App.eventBus.publish('errorEventCodeExists');
        },
        error: function () {
            App.eventBus.publish('eventNotFound', code);
        }
    });
};

svc.getEventPage = function (pageNumber) {
    $.ajax({
        url: '/api/event/page/',
        data: { 'pageNumber': pageNumber },
        dataType: "json",
        contentType: "application/json",
        type: 'GET',
        success: function (data) {
            App.eventBus.publish('loadedNextEventsPage', data);
        }
    });
};

但两者都不必向 API 传递 2 个参数。我猜这真的很小:/

4

2 回答 2

1

您的操作名称称为“Authenticate”,但您已包含以下内容将重命名该操作:

[ActionName("All")]

这使得 URL

/api/event/all
于 2013-07-19T18:37:18.993 回答
0

The problem lies in your url.

Apparently, ajax interpret / at the start of the url to be root

When the application is deployed on serverserver, its URL is something like http://localhost:8080/AppName/

with api/event/page/, ajax resolve the URL to http://localhost:8080/AppName/api/event/page/ or an url relative to your current directory.

However, with /api/event/page/, the URL is resolved to http://localhost:8080/api/event/page/

Hope it helped.

于 2013-07-19T19:07:58.300 回答