Suganya M K Suganya M K - 2 months ago 7
Ajax Question

How to pass a URL as parameter to APIController through AJAX call?

I am trying to pass URL as string parameter to API Controller POST method. However, I am getting 'Potentially dangerous request' error due to the special character ':' in the URL.

Format of the URL will be 'http|s://xxxxxxxxx/.../.... etc.

I tried encodeURI, but it is still failing.

$.ajax({
type: "POST",
url: "http://localhost:101/api/Sample/" + encodeURI(url),
contentType: "application/json; charset=utf-8",
dataType: "json"....
})

Answer

Asuming you want to post your data to "http://localhost:101/api/Sample/" you have to add the encodeURI(url) to the Request body of your post request. Like this for instance.

var formData = {urlParameter:encodeURI(url)}; //Json 
$.ajax({
    url : "http://localhost:101/api/Sample/",
    type: "POST",
    data : formData,
    success: function(data, textStatus, jqXHR)
    {
        //handle success
    },
    error: function (jqXHR, textStatus, errorThrown)
    {
         //Handle error
    }
});

The Sample method that you Post to, must have a parameter corrosponding to the parameter specified in the json in the formData variable.