Harsh Chiki
Harsh Chiki

Reputation: 79

JSON.stringify() alternative and JSON2.js

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.

Upvotes: 4

Views: 5714

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

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.

Upvotes: 3

codeandcloud
codeandcloud

Reputation: 55200

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;",

Upvotes: 1

Related Questions