-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?

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

but I want javascript on ajax call.
undefined output.means if i alert status.value in error function it is undefined.

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.