4

I am trying to make a WCF call to a function which accepts one String parameter. When I pass parameters from jQuery, I am using JSON.stringify(parameters), where parameters is a name:value collection containing the parameters I want to pass.

My doubts are

  1. In my version of IE, JSON is not defined. So, I used the JSON2.js library and included that in the master page of my site.
  2. I still encounter the same message. JSON is undefined on IE.

Well, it works perfectly on Google Chrome.

PS - This is all on .NET.

Script name is json2.js. The values I am passing in jQuery are

data:JSON2.stringify(parameters),
contentType: "application/json2; charset=utf-8",
dataType: "json2"

I am using IE8. (Sorry to not provide this detail before, just added)

Please advise.

Harsh Chiki
  • 79
  • 1
  • 10

2 Answers2

3

Instead of JSON2.stringify(parameters) you should use JSON.stringify(parameters). Also make sure you have included the json2.js script to your site.

And if you are using IE8 you don't need json2.js at all as it natively supports JSON.stringify method.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks Darin. I have added json2.js very well like I did for jQuery library. Somewhere, I am a little afraid why are the definitions not getting linked to json2.js library. I read on github.com, that this .js creates a property of JSON in the global object, and lets us use stringify and parse function. But this is not working here. Please advise. – Harsh Chiki Sep 21 '12 at 07:12
  • I've noticed that you updated your question and stated that you are using IE8. If this is the case you don't need to reference any `json2.js` as IE8 natively supports the `JSON.stringify` method. – Darin Dimitrov Sep 21 '12 at 07:15
  • Well, I will need to make this application compatible for users working on browsers that don't have an implementation for JSON object. – Harsh Chiki Sep 21 '12 at 07:17
  • I see, in this case you need to include it indeed. – Darin Dimitrov Sep 21 '12 at 07:18
  • Thanks Darin, with the suggestions mentioned here, the problem is solved, and I can make the WCF call properly. – Harsh Chiki Sep 21 '12 at 07:30
1

You are using json2 at all places where you should ideally be using json

Please change your ajax call as

data:JSON.stringify(parameters),
contentType: "application/json; charset=utf-8",
dataType: "json"

On an unrelated side-note, you can omit charset and dataType and change the call like this

data:JSON.stringify(parameters),
contentType: "application/json;",
naveen
  • 53,448
  • 46
  • 161
  • 251
  • Thanks Naveen, but I am still encountering the same issue. The following error message is displayed Microsoft JScript runtime error: 'JSON' is undefined Please advise. – Harsh Chiki Sep 21 '12 at 07:08