使用 Javascript 在服务器 URL 上发布 JSON 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14230351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
POST a JSON data on a Server URL using Javascript
提问by Ankit Tanna
I have prepared a JSON data and I need to post it on the server so that a service can be called. URL to the server is available and I am making an AJAX call for the same to POST the data.
我准备了一个 JSON 数据,我需要将它发布到服务器上,以便可以调用服务。服务器的 URL 可用,我正在为相同的 POST 数据进行 AJAX 调用。
But I dont know where to place the JSON string that is generated.
但我不知道在哪里放置生成的 JSON 字符串。
My Code is as Follows:
我的代码如下:
function postJSONData(JSONData, localMode)
{
var localJSONData = JSONData;
var postMode = localMode;
$.ajax({
type: 'POST',
url: 'https://tt.s2.com/tmobile/subscribe-service/uid=ankit_bharat_tanna',
dataType: 'xml',
success: function(data){
alert("SECOND POST JSON DATA");
} // Success Function
}); // AJAX Call
alert("POST JSON ------------> "+localJSONData +" "+postMode);
}
I want to post JSON data to the server URL. any Parameters to be used?
我想将 JSON 数据发布到服务器 URL。要使用的任何参数?
Thanks, Ankit.
谢谢,安吉特。
回答by mbharanidharan88
You should pass the values with data parameter $.ajax() jquery doc link
您应该使用数据参数$.ajax() jquery doc link传递值
function postJSONData(JSONData, localMode)
{
var localJSONData = JSONData;
var postMode = localMode;
$.ajax({
type: 'POST',
url: 'https://tt.s2.com/tmobile/subscribe-service/uid=ankit_bharat_tanna',
contentType:"application/json; charset=utf-8",
dataType:"json"
data: JSONData
success: function(data){
alert("SECOND POST JSON DATA");
} // Success Function
}); // AJAX Call
alert("POST JSON ------------> "+localJSONData +" "+postMode);
}
回答by Amyth
You are missing the data
parameter. Moreover you need to send json
data so dataType
parameter should be set to json
. Below is an Example
您缺少data
参数。此外,您需要发送json
数据,因此dataType
参数应设置为json
. 下面是一个例子
function postJSONData(JSONData, localMode)
{
var localJSONData = JSONData;
var postMode = localMode;
$.ajax({
data: localJSONData,
type: 'POST',
url: 'https://tt.s2.com/tmobile/subscribe-service/uid=ankit_bharat_tanna',
dataType: 'json',
success: function(data){
alert("SECOND POST JSON DATA");
} // Success Function
}); // AJAX Call
alert("POST JSON ------------> "+localJSONData +" "+postMode);
}