Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JQuery gets the instance code for the jason data$.getJSON method


May 08, 2021 JSON



Front desk section:
The code is as follows:
 function SelectProject() {
            var a = new Array;
            var r = window.showModalDialog('SelProject.aspx', a, "dialogWidth=1000px; dialogHeight=600px; resizable: yes");
            if (typeof (r) != 'undefined') {
                var arr = r.split(";");
                $("#hidProjectInnerID").val(arr[0]);
                $("#txtProjectNo").val(arr[1]);
                $.getJSON("../Handler/GetProjectInfor.ashx", { key: "PaymentStatement", InnerID: $("#hidProjectInnerID").val() },
                        function (json) {
                            $("#labFinalCustomer").text(json.finalclient);
                            $("#labOrderNo").text(json.orderno);
                            var strDeviceTr = "";
                            $.each(json.workinghours, function (i, item) {
                                strDeviceTr += "<tr><td><lable name="infor"> " + item.description + "</lable> </td>";
                                strDeviceTr += "<td>   </td>";
                                strDeviceTr += " <td><lable name="infor"> " + item.hoursdays + "</lable></td>";
                                strDeviceTr += "<td>  0.8</td>";
                                strDeviceTr += "<td><lable name="infor"> " + item.workinghour + " </lable></td>";
                                strDeviceTr += "<td>  0.8</td>";
                                strDeviceTr += "<td><lable name="infor"> " + item.workinghour + "</lable></td>";
                                strDeviceTr += "<td>  </td>";
                                strDeviceTr += "</tr>";
                            });
                            $("#infor").append(strDeviceTr);
                        });
            }
        }

ashx
The code is as follows:
string innerid = CommonClass.Request.GetRequest<string>("InnerID", "");
            string key = CommonClass.Request.GetRequest<string>("key", "");
            string result = "";
            if (key == "StockOutApp" && innerid != "")
            {
                result = StockOutApp(innerid);
                context.Response.Write(result);
            }
            else if (key == "PaymentStatement" && innerid != "")
            {
                result = PaymentStatement(innerid);
                context.Response.Write(result);
            }
#region 结算单信息
        public string PaymentStatement(string _innerid)
        {
            try
            {
                string sql = @"select InnerID,pFinalClient,pOrderNo from se_ProjectMain where InnerID='" + _innerid + "'";
                DataTable dt = SqlShift.GetDataTable(sql);
                if (!CommonClass.DTRow.CheckDtIsEmpty(dt))
                {
                    StringBuilder json = new StringBuilder();
                     json.Append(""innerid":""+dt.Rows[0]["InnerID"].ToString()+""");
                     json.Append(","finalclient":"" + dt.Rows[0]["pFinalClient"].ToString() + """);
                     json.Append(","orderno":"" + dt.Rows[0]["pOrderNo"].ToString() + """);
                    json.Append(","workinghours":" + GetWorkingHours(_innerid));
                    return "{" + json.ToString().Trim(',') + "}";
                }
                else
                {
                    return string.Empty;
                }
            }
            catch (Exception ex)
            {
                AppLog.Write("项目获取异常![异常信息:" + ex.Message + "]", AppLog.LogMessageType.Info);
                return string.Empty;
            }
        }
        public string GetWorkingHours(string _innerid)
        {
            try
            {
                string sql = @"select InnerID, wDescription,wWorkingHour,wHours_Days from se_ProjectWorkingHour where wProjectID='" + _innerid + "'";
                DataTable dt = SqlShift.GetDataTable(sql);
                if (!CommonClass.DTRow.CheckDtIsEmpty(dt))
                {
                    StringBuilder json = new StringBuilder();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        json.Append("{");
                        json.Append(""innerid":"" + dt.Rows[0]["InnerID"].ToString() + """);
                        json.Append(","description":"" + dt.Rows[0]["wDescription"].ToString() + """);
                        json.Append(","workinghour":"" + dt.Rows[0]["wWorkingHour"].ToString() + """);
                        json.Append(","hoursdays":"" + dt.Rows[0]["wHours_Days"].ToString() + """);
                        json.Append("},");
                    }
                    return "[" + json.ToString().Trim(',') + "]";
                }
                else
                {
                    return string.Empty;
                }
            }
            catch (Exception ex)
            {
                AppLog.Write("项目获取异常![异常信息:" + ex.Message + "]", AppLog.LogMessageType.Info);
                return string.Empty;
            }
        }
#endregion       

Do a cross-domain ajax request experiment with jquery's $.getJSON

jquery provides a $.getJSON method so that we can implement cross-domain ajax requests, but the content on the jquery API is too little, how to use $.getJSON, what kind of database the request site should return to get $.getJSON, let's illustrate with a practical example.

The back end is php, the following code is mainly implemented one of the functions is to provide an appointment registration interface, the data that needs to be passed in are: user name, contact phone number and address
/?Appointment registration Performs interface/

The code is as follows:
/*预约登记 执行 接口*/ 
case "yuyue_interface": 
$name = trim($_GET['name']); 
$phone = trim($_GET['phone']); 
$addr = trim($_GET['addr']); 
$dt = date("Y-m-d H:i:s"); 
$cb = $_GET['callback']; 
if($name == "" || $name == NULL){ 
echo $cb."({code:".json_encode(1)."})"; 
}elseif($phone == "" || $phone == NULL){ 
echo $cb."({code:".json_encode(2)."})"; 
}elseif($addr == "" || $addr == NULL){ 
echo $cb."({code:".json_encode(3)."})"; 
}else{ 
$db->execute("insert into tb_yuyue (realname,telphone,danwei,dt,ischeck) values ('$name','$phone','$addr','$dt',0)"); 
echo $cb."({code:".json_encode(0)."})"; 
} 
exit; 
break; 

Then there's the front-end processing

The code is as follows:
$(document).ready(function(){ 
//以下3个为预约登记需要的参数 
var name = "name"; //varchar类型,长度最多为8位(4个汉字) 
var phone = "phone"; //varchar类型,长度为11位 
var addr = "addr"; //varchar类型,长度最多为500位(250个汉字) 
$.getJSON("http://请求网站地址/data.php?ac=yuyue_interface&name="+name+"&phone="+phone+"&addr="+addr+"&callback=?", function(data){ 
if(data.code==1){ 
//自定义代码 
alert("姓名不能为空"); 
}else if(data.code==2){ 
//自定义代码 
alert("手机不能为空"); 
}else if(data.code==3){ 
//自定义代码 
alert("所在单位不能为空"); 
}else{ 
//自定义代码 
alert("预约成功"); 
} 
}); 
}); 
It is important to note that in the back-end php code, the "samp;callback" that must be passed in must be passed in. " Also output, such as:
The code is as follows:
$cb = $_GET['callback']; 
echo $cb."({code:".json_encode(4)."})"; 
The above is a simple $.getJSON experiment, through which we can learn how to use $.getJSON, and how to make an interface for others to request across domains.