0

In ASP.NET MVC 3, I am using JSON in my AJAX calls. when i am running my application using visual studio on my development machine application works fine. But If i Publish the same application and try to access it using Virtual Directory i am getting the error JSON is Undefined.

Code

function PopulateData() {

            $.ajax({ url: '../Home/Populate/',//**Error Line**
                type: 'POST',
                    data:  JSON.stringify({
                    'id':@ViewBag.Id                     }),

                    dataType: 'json',
                    contentType: "application/json; charset=utf-8",
                success: function (data) 
                {         
                    //code

                } // ajax callback

            });  // ajax call

        }

Please Reply

tereško
  • 58,060
  • 25
  • 98
  • 150
user1799982
  • 102
  • 1
  • 11

4 Answers4

2

I think problem is physical path in this line

url: '../Home/Populate/'

change it to relative path like this:

url: '@Url.Action("Populate", "Home")' 

Also, you can see your json url with devTools of browsers. And, check it , if your json url is correct.

AliRıza Adıyahşi
  • 15,658
  • 24
  • 115
  • 197
  • Hi AliRıza Adıyahşi Tried using the relative path url: '@Url.Action("Populate","Home")', But still getting the same error – user1799982 Feb 08 '13 at 10:19
2

This is because you have given the url of controller which is local to the project.

It's always better to follow @Url.Action("actionname","controllername") this approach.

         $.ajax({ 
            url: '@Url.Action("Populate","Home")',
            type: 'POST',
            data:  JSON.stringify({id:@ViewBag.Id}),
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            success: function (data) 
            {         
                //code

            } // ajax callback

        });  // ajax call
Karthik Chintala
  • 5,465
  • 5
  • 30
  • 60
1

JSON is Undefined looks more like browser error. There is no built in JSON in old browsers like IE7, FF3.0 etc. So, it looks like you are using different browsers to view your website. Suppose if you remove JSON.stringify it will work fine (not sure why do you need that at all, jquery accepts data as an object and will do everything required to pass it to server correctly):

function PopulateData() {

            $.ajax({ url: '../Home/Populate/',//**Error Line**
                type: 'POST',
                    data:  {'id':@ViewBag.Id },    
                    dataType: 'json',
                    contentType: "application/json; charset=utf-8",
                success: function (data) 
                {         
                    //code

                } // ajax callback

            });  // ajax call

        }

And as it was told in other answers here, you should better use url: @Url.Action("Populate", "Home") instead of relative url like you have it now

Viktor S.
  • 12,736
  • 1
  • 27
  • 52
  • Oh. I see, you are passing it as json. Are you sure you can't use default application/x-www-form-urlencoded ? – Viktor S. Feb 08 '13 at 12:19
  • @user1799982 try to remove `JSON.stringify` and `contentType:...`. Possibly it will work fine, but that depends on server side. – Viktor S. Feb 08 '13 at 12:23
0

Because JSON is not built in all browsers download it https://github.com/douglascrockford/JSON-js and include in you on your website

By the way why you are using stringify for primitive types?

you can do

 data:  { id: @ViewBag.Id }

Then on ASp.Net Mvc Controller side

public ActionResult Populate(int id)

So ASp.net will parse your parameter for you.

Vova Bilyachat
  • 18,765
  • 4
  • 55
  • 80