Invalid JSON primitive: id

后端 未结 2 980
醉话见心
醉话见心 2021-01-08 00:42

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 ju

相关标签:
2条回答
  • 2021-01-08 01:24

    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);
    
    0 讨论(0)
  • 2021-01-08 01:40

    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());
        }
      });
    }

    0 讨论(0)
提交回复
热议问题