使用JQuery发布JSON [英] Posting JSON with JQuery

查看:111
本文介绍了使用JQuery发布JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试获取JQuery将JSON发布到服务器:

Trying to get JQuery to post JSON to a server:

$.ajax({  
  url: "/path/to/url",  
  type: "POST",  
  dataType: "json",  
  contentType: "json",  
  data: {"foo": "bar"},  
  success: function(){              
    alert("success :-)");  
  },  
  error: function(){  
    alert("fail :-(");  
  }  
});  

问题是数据在服务器上显示为"foo=bar"而不是所需的"{\"foo\":\"bar\"}.

Problem is the data appears on the server as "foo=bar" rather than the desired "{\"foo\":\"bar\"}.

我认为指定dataType或contentType参数可以解决问题,但不能.

I thought specifying either the dataType or contentType params would do the trick, but no.

有人知道正确的ajax配置吗? [或另外一种在发布之前将'data'参数序列化为JSON的方法?]

Anyone know the correct ajax configuration ? [or alternatively a way of serialising the 'data' parameter as JSON prior to posting ?]

谢谢!

推荐答案

您可以使用 json2.js :

data: JSON.stringify({"foo": "bar"})

这篇关于使用JQuery发布JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆