서버 코드 예제
서버 코드 예제
다음 섹션은 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
게시판 목록
퍼블리싱강좌
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 118 | jQuery Mobile | 8년 전 | 1350 | ||
| 117 | jQuery Mobile | 8년 전 | 1602 | ||
| 116 | jQuery Mobile | 8년 전 | 1833 | ||
| 115 | jQuery Mobile | 8년 전 | 1786 | ||
| 114 | jQuery Mobile | 8년 전 | 1976 | ||
| 113 | jQuery Mobile | 8년 전 | 1884 | ||
| 112 | jQuery Mobile | 8년 전 | 2492 | ||
| 111 | jQuery Mobile | 8년 전 | 1795 | ||
| 110 | jQuery Mobile | 8년 전 | 1588 | ||
| 109 | jQuery Mobile | 8년 전 | 1750 | ||
| 108 | jQuery Mobile | 8년 전 | 1974 | ||
| 107 | jQuery Mobile | 8년 전 | 1777 | ||
| 106 | jQuery Mobile | 8년 전 | 1456 | ||
| 105 | jQuery Mobile | 8년 전 | 1758 | ||
| 104 | jQuery Mobile | 8년 전 | 2437 | ||
| 103 | jQuery Mobile | 8년 전 | 1568 | ||
| 102 | jQuery Mobile | 8년 전 | 1512 | ||
| 101 | jQuery Mobile | 8년 전 | 1701 | ||
| 100 | jQuery Mobile | 8년 전 | 1928 | ||
| 99 | jQuery Mobile | 8년 전 | 1386 | ||
| 98 | jQuery Mobile | 8년 전 | 1410 | ||
| 97 | jQuery Mobile | 8년 전 | 1665 | ||
| 96 | jQuery Mobile | 8년 전 | 1316 | ||
| 95 | jQuery Mobile | 8년 전 | 1129 | ||
| 94 | jQuery Mobile | 8년 전 | 1532 | ||
| 93 | jQuery Mobile | 8년 전 | 1149 | ||
| 92 | jQuery Mobile | 8년 전 | 1574 | ||
| 91 | jQuery Mobile | 8년 전 | 1525 | ||
| 90 | jQuery Mobile | 8년 전 | 1691 | ||
| 89 | jQuery Mobile | 8년 전 | 1670 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기