[英]Passing string array from ASP.NET to JavaScript
I am trying to call the server side from JavaScript and then pass a string array back to JavaScript, but running into problems. 我试图从JavaScript调用服务器端,然后将字符串数组传递回JavaScript,但是遇到了问题。
// Call the server-side to get the data.
$.ajax({"url" : "MyWebpage.aspx/GetData",
"type" : "post",
"data" : {"IdData" : IdData},
"dataType" : "json",
"success": function (data)
{
// Get the data.
var responseArray = JSON.parse(data.response);
// Extract the header and body components.
var strHeader = responseArray[0];
var strBody = responseArray[1];
// Set the data on the form.
document.getElementById("divHeader").innerHTML = strHeader;
document.getElementById("divBody").innerHTML = strBody;
}
});
On the ASP.Net server side, I have: 在ASP.Net服务器端,我有:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static object GetTip(String IdTip)
{
int iIdTip = -1;
String[] MyData = new String[2];
// Formulate the respnse.
MyData[0] = "My header";
MyData[1] = "My body";
// Create a JSON object to create the response in the format needed.
JavaScriptSerializer oJss = new JavaScriptSerializer();
// Create the JSON response.
String strResponse = oJss.Serialize(MyData);
return strResponse;
}
I am probably mixing things up, as I am still new to JSON. 由于我还是JSON的新手,所以我可能会混淆。
UPDATE with error code: 更新,并显示错误代码:
Exception was thrown at line 2, column 10807 in http://localhost:49928/Scripts/js/jquery-1.7.2.min.js
0x800a03f6 - JavaScript runtime error: Invalid character 0x800a03f6-JavaScript运行时错误:无效字符
Stack trace: parse JSON[jquery-1.7.2.min.js] Line 2 堆栈跟踪:解析JSON [jquery-1.7.2.min.js]第2行
What is my problem? 我怎么了
This s purely out of guess work. 这纯粹是出于猜测。 But see if this is what you are getting:- In your Ajax
call, your data type is json and looking at the method you are returning a json string. 但是,看看这是否是您得到的:-在您的Ajax
调用中,您的数据类型为json,然后查看您要返回的json字符串的方法。 So you do not need to do JSON.parse(data.response). 因此,您不需要执行JSON.parse(data.response)。 Instead just see if the below works for you. 而是只看下面是否适合您。 Also i dont see a response
object in your Json, instead it is just an array. 我也没有在您的Json中看到一个response
对象,相反,它只是一个数组。 So it must be trying to parse undefined
因此它必须尝试解析undefined
var strHeader = data[0];
var strBody = data[1];
I modified your ajax call script to : 我将您的ajax调用脚本修改为:
// Call the server-side to get the data.
$.ajax({
url: "WebForm4.aspx/GetTip",
type: "post",
data: JSON.stringify({ IdTip: "0" }),
dataType: "json",
contentType: 'application/json',
success: function (data) {
// Get the data.
var responseArray = JSON.parse(data.d);
// Extract the header and body components.
var strHeader = responseArray[0];
var strBody = responseArray[1];
// Set the data on the form.
document.getElementById("divHeader").innerHTML = strHeader;
document.getElementById("divBody").innerHTML = strBody;
}
});
Note that I added contentType: 'application/json'
and changed 请注意,我添加了contentType: 'application/json'
并进行了更改
var responseArray = JSON.parse(data.response);
to 至
var responseArray = JSON.parse(data.d);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.