1

In my ASP.NET MVC application I have a problem in calling WebAPI controller by ajax statement.

Here is my code:

    $.ajax({
        url: 'api/cartitems',
        type: self.cartItem.id == null ? 'post' : 'put',
        contentType: 'application/json',
        data: ko.toJSON(data)
    })
    .done(self.successfulSave)
    .fail(self.errorSave)

It produces an error 404 - file not found.

I've tested different possibilities and only one that works is using whole URL path.

    $.ajax({
        url: 'http://xx.yyy.zz.vvv/APP_NAME/api/cartitems',
        type: self.cartItem.id == null ? 'post' : 'put',
        contentType: 'application/json',
        data: ko.toJSON(data)
    })
    .done(self.successfulSave)
    .fail(self.errorSave)

Is it possible to not use the full path?

1

2 Answers 2

1

I wonder whether, there is an error in ASP.NET MVC configuration. On developer environment it works with simplified URL in ajax call. And I can't believe that Microsoft forces developers to adjust URL address on every productive system.

Sign up to request clarification or add additional context in comments.

Comments

0

I bet the Url.Action construct will work. It ties into your routing configuration to produce a valid url.

In .JS Script

 url:'@Url.Action("api","cartitems")',

In .xxhtml

url:'@Model.YourPostabckUrlVariable',

1 Comment

Doh! You are correct. Perhaps putting the postback in the model.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.