user1799982
user1799982

Reputation: 102

JSON is undefined in asp.net mvc

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

Upvotes: 0

Views: 3652

Answers (4)

Vova Bilyachat
Vova Bilyachat

Reputation: 19514

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.

Upvotes: 0

Viktor S.
Viktor S.

Reputation: 12815

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

Upvotes: 1

Karthik Chintala
Karthik Chintala

Reputation: 5545

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

Upvotes: 2

AliRıza Adıyahşi
AliRıza Adıyahşi

Reputation: 15866

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.

Upvotes: 2

Related Questions