ASP.NET Core 2.2 Razor Pages - AJAX Модель вызова страницы не возвращает данные - PullRequest
0 голосов
/ 12 октября 2019

Я новичок в AJAX и не могу даже заставить основы работать правильно.

Я запускаю вызов AJAX из функции в разделе javascript со страницы бритвы, но не могу получить требуемое значение строки, чтобывозвращено.

Результат, который я получаю в окне предупреждения, просто показывает HTML-макет страницы бритвы в качестве сообщения, в отличие от фактической строки, которую я хочу вернуть. Я часами пытался включить X-CSRF-TOKEN в заголовок, но это не имеет значения. Я не уверен, что здесь требуется токен защиты от подделки, и поэтому проблема стоит перед этим шагом.

Страница бритвы:

 $.ajax({
        type: "GET",
        url: "/Maps?handler=CanvasBackgroundImage",
            contentType: 'application/json',
            success: function (result) {
                alert(result);
            }
    });

Модель страницы:

 public JsonResult OnGetCanvasBackgroundImage()
    {
        var result = "test message";
        return new JsonResult(result);
    }

1 Ответ

0 голосов
/ 14 октября 2019

Ниже приведен код, который сейчас работает для меня. Спасибо за всеобщее мнение.

Страница бритвы (раздел скрипта):

 function getMapBackgroundImagePath() {
        // Get the image path from AJAX Query to page model server side.
        $.ajax({
            type: "GET",
            url: "/Maps/Create?handler=MapBackgroundImagePath",
            contentType: "application/json",
            data: { imageId: imageId, }, // Pass the imageId as a string variable to the server side method.
            dataType: "json",
            success: function (response) {
                copyText = response;
                // Run the function below with the value returned from the server.
                renderCanvasWithBackgroundImage();
            },
        });
    }

Модель страницы:

 public JsonResult OnGetMapBackgroundImagePath(string imageId)
    {
        // Get the image full path where the id  = imageId string
        int id = Convert.ToInt32(imageId);
        var imagePath = _context.Image.Where(a => a.ID == id)
            .Select(i => i.ImageSchedule);
        // return the full image path back to js query in razor page script.
        return new JsonResult(imagePath);
    }
...