Jquery Ajax Posting json to webservice

后端 未结 6 600
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 06:15

I am trying to post a JSON object to a asp.net webservice.

My json looks like this:

var markers = { \"markers\": [
  { \"position\": \"128.3657142857         


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 06:41

    1. markers is not a JSON object. It is a normal JavaScript object.
    2. Read about the data: option:

      Data to be sent to the server. It is converted to a query string, if not already a string.

    If you want to send the data as JSON, you have to encode it first:

    data: {markers: JSON.stringify(markers)}
    

    jQuery does not convert objects or arrays to JSON automatically.


    But I assume the error message comes from interpreting the response of the service. The text you send back is not JSON. JSON strings have to be enclosed in double quotes. So you'd have to do:

    return "\"received markers\"";
    

    I'm not sure if your actual problem is sending or receiving the data.

提交回复
热议问题