0

I am new to webservice and JSON. I am developing an application in asp.net which uses Webservices and JSON for Posting data in Ajax call to Server. In the below function PostData I am getting an error at:

 data: "{" + jsonObjectName + ":" + JSON.stringify(dataToSend) + "}",     as "json" is undefined. 

Here dataToSend is an object which contains my data
submitType is submit button id (in page this I have two submit buttons so, I called by id)

strMessagetoShow is text to show success or failure
strMethodToCall which method is called in Webservice?

function PostData(dataToSend, submitType, strMessagetoShow, strMethodToCall,        jsonObjectName) {   
 $.ajax({
    url: window.top.GetWsUrl() + "/" + strMethodToCall,
    type: "POST",
    dataType: "json",
    data: "{" + jsonObjectName + ":" + JSON.stringify(dataToSend) + "}",
    timeout: 30000,     
    contentType: "application/json; charset=utf-8",
    success: function (data) {
        return data;
    },
    error: function (result) {
        alert(result.status + ' ' + result.statusText);
    }
 });
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ravinder Gangadher
  • 1,016
  • 2
  • 12
  • 17

3 Answers3

1

It's JSON.stringify, and JSON.parse with capital letters (javascript is case sensitive).

Also, when using a variable in an object, you have to do:

var obj = {};
    obj[jsonObjectName] = JSON.stringify(dataToSend);

$.ajax({
    ....
    data: obj,
    timeout: 30000,     
    ....etc
});
adeneo
  • 312,895
  • 29
  • 395
  • 388
  • @RavinderGangadher - I'm just saying, any functions related to the JSON object in newer browsers requires you to referrence the correct object, which is JSON with capital letters. – adeneo Dec 29 '12 at 07:23
0

Try this and see if helps:

function PostData(dataToSend, submitType, strMessagetoShow, strMethodToCall, jsonObjectName) {   
 $.ajax({
    url: window.top.GetWsUrl() + "/" + strMethodToCall,
    type: "POST",
    dataType: "json",
    data: {jsonObjectName : JSON.stringify(dataToSend)}, // or $.parseJSON(dataToSend)
    timeout: 30000,     
    contentType: "application/json; charset=utf-8",
    success: function (data) {
        return data;
    },
    error: function (result) {
        alert(result.status + ' ' + result.statusText);
    }
 });
}

What is changed here:

changed this :

  "{" + jsonObjectName + ":" + json.stringify(dataToSend) + "}"

to this:

 {jsonObjectName : JSON.stringify(dataToSend)}
Jai
  • 74,255
  • 12
  • 74
  • 103
  • Can you please mention as comments what you changed.. so that I can understand it..(I am with OP) – Mr_Green Dec 29 '12 at 07:27
  • @Mr_Green - should be obvious, it's the data object. Now it's an object, in the question it's some sort of string with variables, which does'nt work. – adeneo Dec 29 '12 at 07:29
  • @Mr_Green - in what browser ? – adeneo Dec 29 '12 at 07:32
  • I have tested in IE, Chrome and Mozilla – Mr_Green Dec 29 '12 at 07:33
  • And when using `JSON.stringify` Crome and Firefox says `JSON` is undefined if you open the console? – adeneo Dec 29 '12 at 07:36
  • @adeneo The function is executing the **Error** function in Chrome and "json undefined" in IE9 – Mr_Green Dec 29 '12 at 07:39
  • @Mr_Green - Try keeping it simple, like this [**FIDDLE**](http://jsfiddle.net/adeneo/sNmVE/1/), and open the console (F12) and check for errors ! – adeneo Dec 29 '12 at 07:46
  • @RavinderGangadher - The fiddle posted does'nt even use JSON, so that's simply not possible! The console should tell you exactly what line the error is on, are you sure it's not somewhere else in your code? – adeneo Dec 29 '12 at 07:52
  • Just now i checked in console i am getting This errors. 1).Uncaught TypeError: Cannot read property 'Id' of undefined PalletizedWaste.aspx:162 2).OPTIONS http://localhost:12015/WasteManagementWS.asmx/PalletizedWasteRecordExists 500 (Internal Server Error) jquery1.8.3.js:2 XMLHttpRequest cannot load http://localhost:12015/WasteManagementWS.asmx/PalletizedWasteRecordExists. Origin http://localhost:51118 is not allowed by Access-Control-Allow-Origin. – Ravinder Gangadher Dec 29 '12 at 08:10
  • @RavinderGangadher - since there are no attempts to read the id property of anything in the ajax function, that error probably lies elsewhere. The access-control error is Javascripts own origin policy, and you can't access a different domain with ajax, unless you're using JSONP or CORS. – adeneo Dec 29 '12 at 08:24
-1

Make sure you include the JSON library. See https://github.com/douglascrockford/JSON-js/blob/master/json2.js for the API.

JSON.stringify(value, replacer, space)
user1884047
  • 203
  • 1
  • 5