【问题标题】:jQuery AJAX JSON quotations format to WCFjQuery AJAX JSON 引用格式转 WCF
【发布时间】:2012-05-07 02:16:43
【问题描述】:

这是我的示例代码:

var postData = '{"studentId":"' + $("#hidId").val() + '"}';
$.ajax({
   type: 'POST',
   url: 'SomeService.svc/AddId',
   data: postData,
   contentType: 'application/json; charset=utf-8',
   dataType: 'json',
   success: function (msg) {
      //do something...
   }
});

我希望postData的引号保持原样(参数周围有双引号,单代码对我不起作用),但是当浏览器运行它时,所有单引号变成双引号,双引号保持原样,然后变成这样:

"{"studentId":"ST001"}"

我需要这种格式的字符串:

'{"studentId":"ST001"}'

有人知道如何解决这个问题吗?

更新: 自己整理了一下,问题出在我提交回来的数据值上,'studentId'应该是Guid才能匹配WCF服务参数类型。

【问题讨论】:

    标签: c# jquery asp.net ajax json


    【解决方案1】:

    使用转义符\

    var postData = '{"studentId":\'' + $("#hidId").val() + '\'}';
    

    【讨论】:

    • 你能写出你想要的结果吗?例如:'{"studentId":"ST001"}' 或 {"studentId":ST001} 或 {"studentId":"ST001"} 或 {"studentId":'ST001'}?
    • {"studentId":"ST001"}
    【解决方案2】:
    var postData = {};
    postData.studentId = $("#hidId").val();
    

    ...

    data: JSON.stringify(postData),
    

    【讨论】:

    • 字符串化后结果变成这样:""{\"StudentId\":\"ST001\"}""
    • 抱歉,忘了说 postData 需要是一个 javascript 对象。我更新了我的答案。
    • 没有帮助,它仍然给我这个:"{"StudentId":"ST001"}"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 2014-08-15
    • 2012-10-03
    • 1970-01-01
    • 2011-09-06
    相关资源
    最近更新 更多