How to make Ajax call to method from controller

I have Controller PdfViewerController inside of this controller I have a method

private static string GeneratePdfUrl(string test)
{
string aUrl;
//some action//
return aUrl
}

and I have pdfviewercontroller.js inside or this js I want to crete a function that will return a string from Controller using GeneratePdfUrl()

something like this

function _doUrl() {
        $.ajax({
            type: "GET",
            url: ep.utilities.replaceEPCommand('PdfViewer/GeneratePdfUrl'),
            data: { "test": "R"}
        });
    }

what I'm doing wrong. Do I need something like .done ?

I try to create ajax Get function and expected to get string inside of my javascript

Upvotes: 0

Views: 87

Answers (1)

Mahfuzur Rahman
Mahfuzur Rahman

Reputation: 141

Assuming that you have an MVC Home controller

[HttpGet]
    public JsonResult ForAjaxCall(string test)
    {
        var aUrl=GeneratePdfUrl(test);
        return Json(person, JsonRequestBehavior.AllowGet);
    }

This controller will call your custom method

public string GeneratePdfUrl(string test)
{
string aUrl;
//some action//
return aUrl
}

Inside your js file do something like :

function _doUrl() {
        $.ajax({
                type: "GET",
                url: "/Home/ForAjaxCall",
                data: { { "test": "R"} },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    alert("You got your URL "+ response.aUrl);
                },
                failure: function (response) {
                    alert("Something wrong");
                },
                error: function (response) {
                    alert(response.responseText);
                }
            });
    }

Hope this will help you understand the flow.

Upvotes: 1

Related Questions