Click here to Skip to main content
15,936,014 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys I have an issue while trying to make an ajax call in an external js file in MVC. I am trying to call a JSONResult method in my controller. The ajax hits the success function but my JSON result never gets hit. Can someone point me in the right direction.

Hidden field in cshtml to hold the path to the controller
HTML
<input type="hidden" value="@Url.Action("GetNetworkData", "NetworkSetting")" id="NetworkData" />


Js File
JavaScript
var url = $('#NetworkData').val();
      // alert(url);
       var url = $('#NetworkData').data('url');

       $.ajax({
           url: url,
           cache: false,
           type: 'GET',
           contentType: 'application/json; charset=utf-8',
           data: {},
           success: function (data) {
               alert('success');
           }
       });


C#
[HttpGet]
       public JsonResult GetNetworkData()
       {
           string s = "Will return data here";
           return Json(s, JsonRequestBehavior.AllowGet);
       }


I know the url passed to the ajax call is correct as when I paste it directly in the URL it gets called. Any help is very much appreciated.

Thanks..
Posted

1 solution

First try with manual url code if your json is working then put that url as dynamic
And why you have mentioned two time url there I could not understand.
//var url = $('#NetworkData').val();
      // alert(url);
       var url = "ControllName/GetNetworkData";//If it will work then same thing put in networkdata

       $.ajax({
           url: url,
           cache: false,
           type: 'GET',
           contentType: 'application/json; charset=utf-8',
           data: {},
           success: function (data) {
               alert('success');
           }
       });
 
Share this answer
 
Comments
frostcox 6-Jul-14 5:54am    
Ok when I removed the controller name it worked, thanks for your help.
[no name] 6-Jul-14 12:31pm    
welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900