14

I cannot get the following function to work properly. It seems to be serializing it wrong. This is about the 5th iteration of different data variants. I was originally just doing data: {'id': id} like I do at work with WCF, but with the ASMX it just isn't working. It looks like it's serializing teh data as id=1234 instead of id:1234, but I'm fairly new to this. Any help would be appreciated. Oh, and I can call the service directly in the browser and it returns the data properly so I know it's not the service.

function getVentID(id) {
    //look up id in database and get VentID
    alert('id: ' + id);
    var jsdata = { "id": + id}
    $.ajax({
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        url: 'services/UserService.asmx/getVentID',
        data: jsdata,
        dataType: 'json',
        success: function (msg) {
            alert(msg.d);
        },
        error: function (a, b, c) {
            alert('Error: ' + a.toString() + ' ' + b.toString() + " " + c.toString());
        }
    });
}

p.s. I know there are like 10 identical questions but none of them have answers that I could find or that worked for me.

2 Answers2

33

The simplest possible fix would be to change the line beginning var jsdata to:

var jsdata = '{id:' + id + '}';

The problem is that jQuery is encoding jsdata as form data, not as json. The dataType parameter influences how the response is parsed, not how the POST data is encoded.

There's not actually any JSON serialization code in jQuery to the best of my knowledge. Apparently John Resig suggests using Douglas Crockford's json2.js.

To use it, add a script reference to json.js and then:

var jstext = JSON.stringify(jsdata, null, 2);
Community
  • 1
  • 1
sblom
  • 26,911
  • 4
  • 71
  • 95
  • 1
    Well, technically that solved the error I posted, but now I get: Invalid web service call, missing value for parameter: \u0027ID\u0027.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod... etc Any ideas? –  Oct 20 '11 at 04:27
  • I tried changing the case since the new error says ID instead of id but it didn't help and I don't even know if that's related... –  Oct 20 '11 at 04:28
  • Hmmm. I admit I don't quite know exactly what the asmx is expecting. What's an example of what you do directly in your browser that works? I can definitely help translate that to a jQuery call. – sblom Oct 20 '11 at 04:31
  • sblom if you're still around maybe you can update your accepted answer with my adjusted answer to help future googlers. –  Dec 01 '12 at 17:04
0

i solved this problem right now.

You need pass the url in this format:

http://domain.com.br/service.asmx/method?objParam={q : "search"}

And in your service.asmx file, you need declare this method:

Public Function method(objParam As Dictionary(Of String, String)) 

End Function

In your code, looks like:

function getVentID(id) {
  var jsdata = {
    "id": +id
  }
  var sData = JSON.stringify(jsdata); //convert your json in string
  $.ajax({
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    url: 'services/UserService.asmx/getVentID',
    data: {
      id: sData
    },
    dataType: 'json',
    success: function(msg) {
      alert(msg.d);
    },
    error: function(a, b, c) {
      alert('Error: ' + a.toString() + ' ' + b.toString() + " " + c.toString());
    }
  });
}
Sampath
  • 63,341
  • 64
  • 307
  • 441