0

I have this C# code:

    public class HomeController : Controller
    {
        [Route("")]
        public IActionResult Index()
        {
            return View();
        }
        [Route("home/salam")]
        public JsonResult salam()
        {
            return new JsonResult("Alo Alo");
        }
    }

In my Index.cshtml I have this:

`
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="~/lib/jquery/dist/jquery.js"></script>
    <script src="~/lib/jquery-validation/dist/additional-methods.js"></script>
    <script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
    <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
    <div id="ppp">ppopyyyppyoptyphiopy</div>
    <div id="matn"></div>
    <script>
        $("#ppp").click(function () {
        alert("I'm Here");
            $.ajax({
                type: 'GET',
                url: '/home/salam',
                dataType:"jsonp",
                success: function () {
                    alert("Hello Dear");
                }
            });
    </script>
`

It compiles fine, but ajax doesn't work. Please help me. What should I do? The message "I'm here" appears but "Hello Dear" doesn't.

3
  • did you try change dataType:"jsonp" to dataType:"json" ? Commented Apr 16, 2019 at 4:49
  • did you check your console , any error shown there? Commented Apr 16, 2019 at 4:59
  • Thank you. I changed "jsonp" to "json". I also forgot to close the main function by a "});" Commented Apr 16, 2019 at 5:48

2 Answers 2

1

your function is open, close it

function(){
    $.ajax({
                type: 'GET',
                url: '/home/salam',
                dataType:"jsonp",
                success: function () {
                    alert("Hello Dear");
                }
            });
}
Sign up to request clarification or add additional context in comments.

Comments

0

You said:

dataType:"jsonp",

But your server side code says:

return new JsonResult("Alo Alo");

JSON and JSONP are very different.

The success function doesn't fire because it errors when it gets the wrong data type in the response.

Comments

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.