<script type="text/javascript">
$(document).ready(function checkAjax() {
$.ajax({
type: "post",
url: "HighCharts.aspx/DataBank", contentType: "application/json; charset=utf-8", dataType: "json",
success: function (data) {
data = data[1].price;
alert(data);
},
error: function (err) {
alert(err + "调用后台程序出现错误,请尝试刷新!");
}
}
)
})</script>
后台数据:
public static string DataBank()
{
// work=new WorkOrderMachineOnlyOnce();
// iData=(IDataChannels)work;
string ajaxTest = "hello";
string json = "[";
json += "{\"linespeed\":\"" + ajaxTest+ "\"},{\"price\":\"";
json += ajaxTest + "\"}]";
return json;
}
页面打开提示错误时price未定义,各位大大,请教下,这是为啥呢。
$(document).ready(function checkAjax() {
$.ajax({
type: "post",
url: "HighCharts.aspx/DataBank", contentType: "application/json; charset=utf-8", dataType: "json",
success: function (data) {
data = data[1].price;
alert(data);
},
error: function (err) {
alert(err + "调用后台程序出现错误,请尝试刷新!");
}
}
)
})</script>
后台数据:
public static string DataBank()
{
// work=new WorkOrderMachineOnlyOnce();
// iData=(IDataChannels)work;
string ajaxTest = "hello";
string json = "[";
json += "{\"linespeed\":\"" + ajaxTest+ "\"},{\"price\":\"";
json += ajaxTest + "\"}]";
return json;
}
页面打开提示错误时price未定义,各位大大,请教下,这是为啥呢。
解决方案 »
- 请帮我看看这两段代码里面动态生成按钮事件的绑定的问题!!!
- 关于给图片设置路径的问题
- 紧急求助,服务器控件textbox能做光标焦点自动跳转吗?
- 关于使用VSS的问题
- 【难】几个问题,希望有项目开发经验的兄弟给与指导!!! 跟画面无关的问题。
- 当前页面是utf-8编码是,保存进数据库时是否需要对数据进行编码?
- 在自定义控件中使用 linkbutton 在三级页面不能点击链接!!
- 求救,迪杰斯特拉算法实现公交查询
- 没办法,客户喜欢动画类,漂亮的网站,刚才做了一个,大家看值多少钱,谢谢!http://www.hualine.com/zhaoshang/index.html
- 用javascript 如何控制服务器控件的属性?
- 我用了无刷新联动,在插入的时候怎么取值?
- asp.net 发布出错
2、data.d :返回的json在这里
然后 r[1].price
alert(sss[1]["price"]);
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function CallCs() {
$.ajax({
//要用post方式
type: "Post",
//方法所在页面和方法名
url: "WebForm1.aspx/GetJson",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
//返回的数据用data.d获取内容
alert(data.d);
},
error: function (err) {
alert(err);
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" onclick="CallCs()" value="test" />
</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } [WebMethod]
public static string GetJson()
{
return "Hello world";
}
}
}
如果是json格式的字符串
前台要这样处理:
json = eval("("+jsonstr+")");