서버 코드 예제
서버 코드 예제
다음 섹션은 SQL 데이터를 가져 오는 데 사용되는 서버 코드 목록입니다.
1. PHP와 MySQL 사용하기. JSON 반환.
2. PHP와 MS Access 사용하기. JSON 반환.
3. ASP.NET, VB 및 MS Access 사용. JSON 반환.
4. ASP.NET, Razor 및 SQL Lite 사용. JSON 반환.
교차 사이트 HTTP 요청
요청하는 페이지가 아닌 다른 서버의 데이터 요청은 사이트 간 HTTP 요청 이라고 합니다.
교차 사이트 요청은 웹에서 일반적입니다. 많은 페이지가 다른 서버의 CSS, 이미지 및 스크립트를로드합니다.
최신 브라우저에서는 보안상의 이유로 스크립트의 사이트 간 HTTP 요청 이 동일한 사이트 로 제한됩니다 .
다음 예제는 PHP 예제에서 사이트 간 액세스를 허용하도록 추가되었습니다.
header("Access-Control-Allow-Origin: *");
1. 서버 코드 PHP와 MySQL
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$conn = new mysqli("myServer", "myUser", "myPassword", "Northwind");
$result = $conn->query("SELECT CompanyName, City, Country FROM Customers");
$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
if ($outp != "") {$outp .= ",";}
$outp .= '{"Name":"' . $rs["CompanyName"] . '",';
$outp .= '"City":"' . $rs["City"] . '",';
$outp .= '"Country":"'. $rs["Country"] . '"}';
}
$outp ='{"records":['.$outp.']}';
$conn->close();
echo($outp);
?>
2. 서버 코드 PHP 및 MS 액세스
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=ISO-8859-1");
$conn = new COM("ADODB.Connection");
$conn->open("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=Northwind.mdb");
$rs = $conn->execute("SELECT CompanyName, City, Country FROM Customers");
$outp = "";
while (!$rs->EOF) {
if ($outp != "") {$outp .= ",";}
$outp .= '{"Name":"' . $rs["CompanyName"] . '",';
$outp .= '"City":"' . $rs["City"] . '",';
$outp .= '"Country":"'. $rs["Country"] . '"}';
$rs->MoveNext();
}
$outp ='{"records":['.$outp.']}';
$conn->close();
echo ($outp);
?>
3. 서버 코드 ASP.NET, VB 및 MS 액세스
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%
Response.AppendHeader("Access-Control-Allow-Origin", "*")
Response.AppendHeader("Content-type", "application/json")
Dim conn As OleDbConnection
Dim objAdapter As OleDbDataAdapter
Dim objTable As DataTable
Dim objRow As DataRow
Dim objDataSet As New DataSet()
Dim outp
Dim c
conn = New OledbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=Northwind.mdb")
objAdapter = New OledbDataAdapter("SELECT CompanyName, City, Country FROM Customers", conn)
objAdapter.Fill(objDataSet, "myTable")
objTable=objDataSet.Tables("myTable")
outp = ""
c = chr(34)
for each x in objTable.Rows
if outp <> "" then outp = outp & ","
outp = outp & "{" & c & "Name" & c & ":" & c & x("CompanyName") & c & ","
outp = outp & c & "City" & c & ":" & c & x("City") & c & ","
outp = outp & c & "Country" & c & ":" & c & x("Country") & c & "}"
next
outp ="{" & c & "records" & c & ":[" & outp & "]}"
response.write(outp)
conn.close
%>
4. Server Code ASP.NET, Razor C # 및 SQL Lite
@{
Response.AppendHeader("Access-Control-Allow-Origin", "*")
Response.AppendHeader("Content-type", "application/json")
var db = Database.Open("Northwind");
var query = db.Query("SELECT CompanyName, City, Country FROM Customers");
var outp =""
var c = chr(34)
}
@foreach(var row in query){
if (outp != "") {outp = outp + ","}
outp = outp + "{" + c + "Name" + c + ":" + c + @row.CompanyName + c + ","
outp = outp + c + "City" + c + ":" + c + @row.City + c + ","
outp = outp + c + "Country" + c + ":" + c + @row.Country + c + "}"
}
outp ="{" + c + "records" + c + ":[" + outp + "]}"
@outp
다음 섹션은 SQL 데이터를 가져 오는 데 사용되는 서버 코드 목록입니다.
1. PHP와 MySQL 사용하기. JSON 반환.
2. PHP와 MS Access 사용하기. JSON 반환.
3. ASP.NET, VB 및 MS Access 사용. JSON 반환.
4. ASP.NET, Razor 및 SQL Lite 사용. JSON 반환.
교차 사이트 HTTP 요청
요청하는 페이지가 아닌 다른 서버의 데이터 요청은 사이트 간 HTTP 요청 이라고 합니다.
교차 사이트 요청은 웹에서 일반적입니다. 많은 페이지가 다른 서버의 CSS, 이미지 및 스크립트를로드합니다.
최신 브라우저에서는 보안상의 이유로 스크립트의 사이트 간 HTTP 요청 이 동일한 사이트 로 제한됩니다 .
다음 예제는 PHP 예제에서 사이트 간 액세스를 허용하도록 추가되었습니다.
header("Access-Control-Allow-Origin: *");
1. 서버 코드 PHP와 MySQL
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$conn = new mysqli("myServer", "myUser", "myPassword", "Northwind");
$result = $conn->query("SELECT CompanyName, City, Country FROM Customers");
$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
if ($outp != "") {$outp .= ",";}
$outp .= '{"Name":"' . $rs["CompanyName"] . '",';
$outp .= '"City":"' . $rs["City"] . '",';
$outp .= '"Country":"'. $rs["Country"] . '"}';
}
$outp ='{"records":['.$outp.']}';
$conn->close();
echo($outp);
?>
2. 서버 코드 PHP 및 MS 액세스
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=ISO-8859-1");
$conn = new COM("ADODB.Connection");
$conn->open("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=Northwind.mdb");
$rs = $conn->execute("SELECT CompanyName, City, Country FROM Customers");
$outp = "";
while (!$rs->EOF) {
if ($outp != "") {$outp .= ",";}
$outp .= '{"Name":"' . $rs["CompanyName"] . '",';
$outp .= '"City":"' . $rs["City"] . '",';
$outp .= '"Country":"'. $rs["Country"] . '"}';
$rs->MoveNext();
}
$outp ='{"records":['.$outp.']}';
$conn->close();
echo ($outp);
?>
3. 서버 코드 ASP.NET, VB 및 MS 액세스
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%
Response.AppendHeader("Access-Control-Allow-Origin", "*")
Response.AppendHeader("Content-type", "application/json")
Dim conn As OleDbConnection
Dim objAdapter As OleDbDataAdapter
Dim objTable As DataTable
Dim objRow As DataRow
Dim objDataSet As New DataSet()
Dim outp
Dim c
conn = New OledbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=Northwind.mdb")
objAdapter = New OledbDataAdapter("SELECT CompanyName, City, Country FROM Customers", conn)
objAdapter.Fill(objDataSet, "myTable")
objTable=objDataSet.Tables("myTable")
outp = ""
c = chr(34)
for each x in objTable.Rows
if outp <> "" then outp = outp & ","
outp = outp & "{" & c & "Name" & c & ":" & c & x("CompanyName") & c & ","
outp = outp & c & "City" & c & ":" & c & x("City") & c & ","
outp = outp & c & "Country" & c & ":" & c & x("Country") & c & "}"
next
outp ="{" & c & "records" & c & ":[" & outp & "]}"
response.write(outp)
conn.close
%>
4. Server Code ASP.NET, Razor C # 및 SQL Lite
@{
Response.AppendHeader("Access-Control-Allow-Origin", "*")
Response.AppendHeader("Content-type", "application/json")
var db = Database.Open("Northwind");
var query = db.Query("SELECT CompanyName, City, Country FROM Customers");
var outp =""
var c = chr(34)
}
@foreach(var row in query){
if (outp != "") {outp = outp + ","}
outp = outp + "{" + c + "Name" + c + ":" + c + @row.CompanyName + c + ","
outp = outp + c + "City" + c + ":" + c + @row.City + c + ","
outp = outp + c + "Country" + c + ":" + c + @row.Country + c + "}"
}
outp ="{" + c + "records" + c + ":[" + outp + "]}"
@outp
게시판 목록
퍼블리싱강좌
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 208 | 부트스트랩 | 8년 전 | 2725 | ||
| 207 | AngularJS | 8년 전 | 1468 | ||
| 206 | AngularJS | 8년 전 | 1112 | ||
| 205 | AngularJS | 8년 전 | 986 | ||
| 204 | AngularJS | 8년 전 | 1390 | ||
| 203 | AngularJS | 8년 전 | 1184 | ||
| 202 | AngularJS | 8년 전 | 1499 | ||
| 201 | AngularJS | 8년 전 | 1449 | ||
| 200 | AngularJS | 8년 전 | 1428 | ||
| 199 | AngularJS | 8년 전 | 1346 | ||
| 198 | AngularJS | 8년 전 | 1230 | ||
| 197 | AngularJS | 8년 전 | 1393 | ||
| 196 | AngularJS | 8년 전 | 1045 | ||
| 195 | AngularJS | 8년 전 | 1259 | ||
| 194 | AngularJS | 8년 전 | 1268 | ||
| 193 | AngularJS | 8년 전 | 1615 | ||
| 192 | AngularJS | 8년 전 | 1361 | ||
| 191 | AngularJS | 8년 전 | 1588 | ||
| 190 | AngularJS | 8년 전 | 1552 | ||
| 189 | AngularJS | 8년 전 | 1645 | ||
| 188 | AngularJS | 8년 전 | 1401 | ||
| 187 | AngularJS | 8년 전 | 1369 | ||
| 186 | AngularJS | 8년 전 | 1406 | ||
| 185 | AngularJS | 8년 전 | 1440 | ||
| 184 | AngularJS | 8년 전 | 1483 | ||
| 183 | AngularJS | 8년 전 | 1479 | ||
| 182 | AngularJS | 8년 전 | 1008 | ||
| 181 | AngularJS | 8년 전 | 1388 | ||
| 180 | AngularJS | 8년 전 | 1277 | ||
| 179 | AngularJS | 8년 전 | 1256 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기