【问题标题】:JSON calls not working after switching type to POST instead of GET将类型切换为 POST 而不是 GET 后 JSON 调用不起作用
【发布时间】:2011-11-04 21:00:31
【问题描述】:

我一直在使用带有 ASP.NET 的 JayRock 框架来返回通过 javascript 使用的 JSON。一切正常,直到我将 $.ajax 调用从 GET 更改为 POST。进行该更改后,我现在收到此错误。

{"id":null,"error":{"name":"JSONRPCError","message":"Missing value.","errors":[{"name":"JsonException","message":"Missing value."}]}}

这是我的 javascript:

    var tmp = '{ "assID": 52 }';
    var tmpObj = $.parseJSON(tmp);


$.ajax
({
    type: "POST",
    url: '/jsonC.ashx/tester',
    dataType: 'json',
    data: tmpObj,
    async: true,
    contentType: 'application/json',
   success: function (result) {
        console.log(JSON.stringify(result));
    }
})

有人有什么想法吗? 提前致谢。

【问题讨论】:

  • 该网址是否希望收到帖子?如果它只查找 GET 值,那么您仍然需要在 URL 中传递参数。
  • 和 Marc 的想法一样,但是正确的做法是从 POST 获取服务器端的参数,而不是在 URL 中冗余发送。
  • 我想我不明白,我正在使用 Jayrock,我认为它可以为我处理差异?我似乎找不到任何设置..我整天用谷歌搜索这个问题

标签: jquery json jayrock


【解决方案1】:

这里是应该做什么的定义:

var dataString = '{ "assID": 52 }'; 
var postData = $.parseJSON(dataString);
var response;
$.ajax({
  type: 'POST',
  url: '/jsonC.ashx/tester',
  contentType: 'application/json',
  data: JSON.stringify(postData),
  dataType: 'json',
  success: function(data) {
      //do something for success if you want. If your response is JSON:
      response = $.parseJSON(data)
  },
  error: function(data) {
      //do somtething for error if you want. If your response is JSON:
      response = $.parseJSON(data)
  }
});

【讨论】:

  • 实际上我正在尝试发送数据,但简化了代码只是为了尝试从帖子中获得响应.. 这是数据.. var tmp = '{ "assID": 52 }' ; var tmpObj = $.parseJSON(tmp);然后在ajax调用中..数据:tmpObj,
  • 取得进展...现在我收到一个服务器错误,我可以通过 firebug 看到。“尚不支持通知。” Jayrock 问题?
  • 你的问题是:If the ID is not there or was not set then this is a notification request from the client that does not expect any response. Right now, we don't support this. 从这里:java2s.com/Open-Source/CSharp/Development/Jayrock/Jayrock/…
  • 所以这意味着您应该将id 设置为某个值。我的意思是: var dataString = '{ "id" : 1, "assID": 52 }';
  • 哦,太好了@kamaci 所以,我包含了 id,它抱怨没有方法调用,所以我包含了方法的名称.. 现在它的消息是:“参数计数不匹配。”。 ..您有机会知道包含该参数的语法..我尝试过的一切都失败了..谢谢!
【解决方案2】:

您需要将参数作为数据添加到配置值中...

$.ajax
({
    type: "POST",
    url: '/jsonC.ashx/tester',
    dataType: 'json',
    data: {id: some_value, other_var: 'some string'}
    async: true,
    contentType: 'application/json',
   success: function (result) {
        console.log(JSON.stringify(result));
    }
})

只需将数据的内容替换为您的实际值。就目前而言,您正在发布到页面但不发送任何帖子变量。

【讨论】:

    猜你喜欢
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    相关资源
    最近更新 更多