3

I use the following ajax script.

$.ajax({
            dataType: 'json',
            url: url,
            data:  tuDispId,
            type: "GET",
            success: function (data) {
                bindData(data);
                $("#alert-placeholder").empty();
                $('#alert-placeholder').removeClass('alert alert-danger');
            },
            error: function (xhr, textStatus, errorThrown) {
                $('#alert-placeholder').addClass('alert alert-danger');
                $('#alert-placeholder').html(errorThrown);
            }
        });

The attribute Route in Web API before method.

[Route("api/tudisp/Edit/{tuDispId}")]
        public IHttpActionResult Edit(int tuDispId)
{
}

The genarated request from ajax.

http://localhost:xxxxx/api/tudisp/Edit/?179

How to force ajax to not generate sign '?' by id parameter.

3
  • 1
    Because the method is GET Commented Nov 17, 2017 at 13:13
  • You have to write the data parameter like this on your request: data: { id: tuDispId } Commented Nov 17, 2017 at 13:14
  • url:url+"/"+tuDispId, Commented Nov 17, 2017 at 13:14

1 Answer 1

4

The simplest way to do it is to change the url property of the Ajax options...

$.ajax({
    dataType: 'json',
    url: "http://localhost:xxxxx/api/tudisp/Edit/" + tuDispId,
    type: "GET",
    success: function (data) {
        bindData(data);
        $("#alert-placeholder").empty();
        $('#alert-placeholder').removeClass('alert alert-danger');
    },
    error: function (xhr, textStatus, errorThrown) {
        $('#alert-placeholder').addClass('alert alert-danger');
        $('#alert-placeholder').html(errorThrown);
    }
});

GET parameters are automatically appended to the Url as a querystring, which is fine if that's what your application is expecting, but not when you're using routing as you are.

However, if you wanted to modify existing Ajax requests you can use prefiltering. This example modifies the Url of the ajax call, replacing {variable} with a given value from the data object...

$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
    options.data = ""; // this removes the querystring
    for (key in originalOptions.data) {
        options.url = options.url.replace("{" + key + "}", originalOptions.data[key]);
    }
});

$.ajax({
    url: "http://localhost:xxxxx/api/tudisp/Edit/{tuDispId}",
    data: {
        "tuDispId": 179
    }
});

If you wanted to use something like that then I'd strongly recommend spending a bit of time making it more robust, but it's an example of how you could do what you want.

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

2 Comments

This solution I have already tested, I was wonder wheather in section data is possible. Thanks
There you go - I've added something that may be what you're looking for. I quite like it actually and may use it myself in future :D

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.