[英]Getting an error “ 'JSON' is undefined ” in webservices
I am new to webservice and JSON. 我是Web服务和JSON的新手。 I am developing an application in asp.net which uses Webservices and JSON for Posting data in Ajax call to Server.
我正在asp.net中开发一个应用程序,该应用程序使用Webservices和JSON在服务器的Ajax调用中发布数据。 In the below function PostData I am getting an error at:
在下面的函数PostData中,我得到了一个错误:
data: "{" + jsonObjectName + ":" + JSON.stringify(dataToSend) + "}", as "json" is undefined.
Here dataToSend is an object which contains my data 这里dataToSend是一个包含我的数据的对象
submitType is submit button id (in page this I have two submit buttons so, I called by id) SubmitType是提交按钮ID(在此页面中,我有两个提交按钮,所以我用ID进行了调用)
strMessagetoShow is text to show success or failure strMessagetoShow是显示成功或失败的文本
strMethodToCall which method is called in Webservice? strMethodToCall在Webservice中调用哪个方法?
function PostData(dataToSend, submitType, strMessagetoShow, strMethodToCall, jsonObjectName) {
$.ajax({
url: window.top.GetWsUrl() + "/" + strMethodToCall,
type: "POST",
dataType: "json",
data: "{" + jsonObjectName + ":" + JSON.stringify(dataToSend) + "}",
timeout: 30000,
contentType: "application/json; charset=utf-8",
success: function (data) {
return data;
},
error: function (result) {
alert(result.status + ' ' + result.statusText);
}
});
}
It's JSON.stringify
, and JSON.parse
with capital letters (javascript is case sensitive). 它是
JSON.stringify
和带有大写字母的JSON.parse
(javascript区分大小写)。
Also, when using a variable in an object, you have to do: 另外,在对象中使用变量时,您必须执行以下操作:
var obj = {};
obj[jsonObjectName] = JSON.stringify(dataToSend);
$.ajax({
....
data: obj,
timeout: 30000,
....etc
});
Try this and see if helps: 试试这个,看看是否有帮助:
function PostData(dataToSend, submitType, strMessagetoShow, strMethodToCall, jsonObjectName) {
$.ajax({
url: window.top.GetWsUrl() + "/" + strMethodToCall,
type: "POST",
dataType: "json",
data: {jsonObjectName : JSON.stringify(dataToSend)}, // or $.parseJSON(dataToSend)
timeout: 30000,
contentType: "application/json; charset=utf-8",
success: function (data) {
return data;
},
error: function (result) {
alert(result.status + ' ' + result.statusText);
}
});
}
What is changed here: 这里有什么变化:
changed this : 改变了这个:
"{" + jsonObjectName + ":" + json.stringify(dataToSend) + "}"
to this: 对此:
{jsonObjectName : JSON.stringify(dataToSend)}
Make sure you include the JSON library. 确保包含JSON库。 See https://github.com/douglascrockford/JSON-js/blob/master/json2.js for the API.
有关API,请参见https://github.com/douglascrockford/JSON-js/blob/master/json2.js 。
JSON.stringify(value, replacer, space)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.