-2

I am returning javascript from controller to view through ajax. but it gives me failure message, my code is given below.

My controller is

public ActionResult GetJava()
{
    if (Request.IsAjaxRequest())
    {
        return JavaScript("<script>alert(\"some message\")</script>");
    }
    else
    {
        return View();
    }
}

and view is

@{
    ViewBag.Title = "GetJava";
    Scripts.Render("~/bundles/jquery");
}

<h2>GetJava</h2>
<div><input type="button" value="GetJava" onclick="Getjavascript()" /></div>
<script type="text/javascript">

function Getjavascript() {
    $.ajax({
        url: '@Url.Action("GetJava","BindingJson")',
        dataType: 'applicaiton/javascript;charset=utf-8',
        type: 'Get',
        data: 'script',
        success: function (status) { status.value },
        error: function (status) { alert("failure") }
    });
}
</script>

What could be the possible issue?

ekad
  • 14,436
  • 26
  • 44
  • 46
Ali Khan
  • 21
  • 4

1 Answers1

0

You can create a script with src to your action, as suggested in this answer: https://stackoverflow.com/a/6778069/4251546

var script = $('<script />', {src: '@Url.Action("GetJava","BindingJson")'});
$(document).append(script)
Community
  • 1
  • 1
Vsevolod Goloviznin
  • 12,074
  • 1
  • 49
  • 50