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

4
  • did you add json ui files or any necessary document? Commented Feb 8, 2013 at 9:57
  • What browsers are you using? Commented Feb 8, 2013 at 9:58
  • What browser are you using to view your sites? Commented Feb 8, 2013 at 9:59
  • If you are using the server browser or the browsing capability built into IIS manager (I'm not sure what you mean here by using Virtual Directory) than it is an very old IE engine and it does not have built in JSON object. Commented Feb 8, 2013 at 9:59

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi AliRıza Adıyahşi Tried using the relative path url: '@Url.Action("Populate","Home")', But still getting the same error
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

8 Comments

Hi Karthik, Tried using the relative path url: '@Url.Action("Populate","Home")', But still getting the same error
@user1799982 what is the browser that you are using
@user1799982 check this if you are using IE < 8
Hi Karthik My browser is IE 8 I tried removing JSON.Stringify then data is not fetched.
@user1799982 So, data option in $.ajax is data: ({ id: @ViewBag.Id }). Is it the same ?
|
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

2 Comments

Oh. I see, you are passing it as json. Are you sure you can't use default application/x-www-form-urlencoded ?
@user1799982 try to remove JSON.stringify and contentType:.... Possibly it will work fine, but that depends on server side.
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.

1 Comment

Thanks all..I Refered the link sitepoint.com/javascript-json-serialization

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.