0

I have this simple ajax method:

$.ajax({
     type: 'POST',
     url: 'http://localhost:1195/widget/postdata',
     datatype: "jsondata",
     async: false,
     success: function (data) {
          alert("ok")
     },
     error: function (data) {
          alert(data.status + ' ' + data.statusText);
     }
});

And this simple method in c#:

[HttpPost]
public JsonResult PostData()
{
     return Json("1");
}

When I check the inspector console I have "1" in response but my ajax method always goes to error function.

Why is it so?

Hamid Reza
  • 2,913
  • 9
  • 49
  • 76

2 Answers2

1

In your AJAX call it should be dataType: "json" and not datatype: "jsondata". You can refer to the docs

Also use @Url.Action tag in your url call: url: '@Url.Action("postdata", "widget")'

For a small test, I have used the following AJAX call to the same controller method that you have posted and I am getting the correct result:

    $(document).ready(function () {
        $("#button_1").click(function (e) {
        e.preventDefault();
        $.ajax({
            type: "POST",
            url:'@Url.Action("PostData", "Home", null, Request.Url.Scheme)',
            datatype: "json",
            async: false,
            success: function (result) {
                alert(result);
            },
            error: function (error) {
                alert(error);
            }
        });
      });
    });
</script>

<button id="button_1" value="val_1" name="but1">Check response</button>

Output:

enter image description here

Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54
  • Even this can not solve my problem because its referring to a local url. – Hamid Reza Dec 30 '19 at 07:13
  • @HamidReza Try this overload: `@Url.Action("postdata", "widget", null, Request.Url.Scheme)`. More information here: https://stackoverflow.com/questions/434604/how-do-i-find-the-absolute-url-of-an-action-in-asp-net-mvc – Rahul Sharma Dec 30 '19 at 07:18
  • The issue is not about the url at all. The problem is the request is recieved in destination server and response is received in source server but the ajax method goes to error function. – Hamid Reza Dec 30 '19 at 07:52
  • @HamidReza I have made a sample project in which I am using the `AJAX` call and the same controller method that you have. I am getting the correct response from it. Please see updated answer. Do you have a snippet of the error response that your receive? – Rahul Sharma Dec 30 '19 at 08:24
  • Can you send your ajax method to another website address? Lets saty your application domain is aaa.com. Can ypu send ajax request from aaa.com to bbb.com and receive data? – Hamid Reza Dec 30 '19 at 08:35
  • @HamidReza The above example assumes that your ActionMethod is in the same project. If you want to call an external API from a different domain then you would have to use a `HttpClient` in your `Controller` or you could called it directly and get the response. Can you show me the external API that you are trying to call? – Rahul Sharma Dec 30 '19 at 08:44
  • @HamidReza Since you are calling an external API, Maybe the data being returned by the ajax call does not match the dataType: "json". Can you please show me the error response that you get? – Rahul Sharma Dec 30 '19 at 08:45
0

Change the Ajax request to the following :

$.ajax({
 type: 'POST',
 url: '/widget/postdata',
 datatype: "json",
 async: false,
 success: function (data) {
      console.log(data);// to test the response
 },
 error: function (data) {
      alert(data.status + ' ' + data.statusText);
 }
});

Always use relative URL instead of full URL that contains the domain name, this will make the deployment easier so you don't have to change the URls when going live

Cyber Progs
  • 3,656
  • 3
  • 30
  • 39