2021年6月13日 星期日

Views

JavaScript 和Jquery差異比較

 1.在區塊放入html元素字串 有<div id='cards'></div>

 js:

document.queryselect('#cards').innerHTML='<p>文字</p>';


jquery:

 $('#cards').html('<p>文字</p>');

2021年6月4日 星期五

Views

API 傳值和回傳

 目的:練習使用json.html和json.aspx發出ajax請求,並使用json.ashx(沒有畫面的api)返回json物件格式


1.json.html語法

<!DOCTYPE html>
    <head >
        <title>JSON</title>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>                        
    </head>
<body>
    <H5>Hwllo World</H5>

    <script>
    $.ajax({
        url: "json.ashx",
        data: {"id":"0125","name":"Tina"},
        type: "POST",
        dataType: "json",
        success: function(returnData){
            console.log(returnData);
        },
        error: function(xhr, ajaxOptions, thrownError){
            console.log(xhr.status);
            console.log(thrownError);
        }
    });

    </script>
</body>
</html>

$.ajax各屬性代表意思
(1).請求ashx檔案的時候 要把contentType去掉,還有就是
data 格式為 {”key”,”value”};切記 不要再 大括號外面加雙引號,這樣就會在ashx頁面取不到資料而失敗。
(2).
contentType
contentType是網頁要送到Server的資料型態,若沒指定則預設為'application/x-www-form-urlencoded; charset=UTF-8'
dataType
dataType是網頁預期從Server接收的資料型態,若沒指定則jQuery會根據response的MIME type來推定為xml, json, script, html, text。

2.json.aspx語法code behind要有json.aspx.cs就不放上

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Template.aspx.cs" Inherits="Template" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head >
    <title>Ch2</title>
<title>Bootstrap Navigation Bar</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
 

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>                     
</head>

<body >
<h1>This is Empty</h1>

<form runat="server">

</form>


<script>


$.ajax({
    url: "json.ashx",
    data: {"id":"0125","name":"Tina"},
    type: "POST",
    dataType: "json",
    success: function(returnData){
        console.log(returnData);
    },
    error: function(xhr, ajaxOptions, thrownError){
        console.log(xhr.status);
        console.log(thrownError);
    }
});


</script>

</body>
</html>

3.調用json.ashx的API


<%@ WebHandler Language = "C#" Class = "ResponseOds" %>
using System;
using System.Web;
using System.Collections.Generic;
using System.IO;
using System.Web.Script.Serialization;
using Newtonsoft.Json;

public class ResponseOds : IHttpHandler
{
	
    public void ProcessRequest (HttpContext context)
    {
        
        string id=context.Request["id"]; //接到前端傳來值
        if(id=="0125") //判斷是0125的值,在將相對應要回傳值丟到前台
        {
            List<Student> lstStuModel = new List<Student>()
            {
                new Student(){ID=1,Name="張飛",Age=250,Sex="男"},
                new Student(){ID=2,Name="潘金蓮",Age=300,Sex="女"}
            };

            //Newtonsoft.Json序列化
            string jsonData = JsonConvert.SerializeObject(lstStuModel);

            context.Response.ContentType = "application/json"; //回傳json格式
            context.Response.Charset = "utf-8";
            context.Response.Write(jsonData);
        }

        
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }



    class Student
    {
        public int ID { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }

        public string Sex { get; set; }
    }

參考資料 (1).(2).(3).

2021年5月21日 星期五

Views

抓取jquery下拉選單值的問題

 前端:每次要給下拉選單selectedIndex,值給到但是顯示確未變

原因:因為Boothtriap4 的顯示內容沒有更改到

解法:只要抓到顯示內容地方修改後就可以。

程式碼:


<select id="Num">
  <option>請選擇數字</option>
  <option>1</option>
  <option>2</option>
</select>
  <button>
     <div class="filter-option">
        <div class="filter-option-inner"> 
          <div class="filter-option-inner-inner">
            取餐囉 
          </div>       
        </div>
     </div>
</button>
let CityName=$(this).children(":first").data('info'); //取到目前抓取的jQuery物件,它子元素的第一個data-info的值
$("#Num")[0].selectedIndex =parseInt($('#CityIndex').val()); // 給予下拉選單選取值
$('#Num').next().children().children().children().text(CityName);  //最後抓取下拉旁邊的按鈕它的子元素子元素子元素內容進行修改
//想到更簡潔的寫法為利用find的方法,到按鈕內遍歷它的子元素,找到有div class="filter-option-inner-inner" DOM物件
$('#Num').next().find('div .filter-option-inner-inner').text('找到了')
//不同children()方法只能抓到第一個子元素


2021年5月8日 星期六

Views

工作架設本地端IIS所遇到問題

 前言

安裝本地端IIS所遇到的問題

1.IIS基礎安裝

2.rewrite功能尚未安裝



遇到這個錯誤表示IIS的rewrite功能尚未安裝
解決方式:1.參考文章2.參考文章

3.Asp.Net相關設定要瞭解,例如參考文章SessionTimOut

4.架設好後,一直跳出需要debugger視窗

上圖:這個需要使用即時偵錯工具,來查看程式碼

為什麼使用即時偵錯工具,不能附加id調用有錯誤,因為進程id會掉,要在網址上打上相關網頁多試幾次就能成功,參考附加進程觀念










Views

MS SQL資料備份

 前言

如何快速備份資料

1.打開產生指令碼工具

   


2.重點是要調整編輯指令碼的資料類型

   (1)結構描述和資料 (建表和資料) (2)僅限結構描述(建表)(3)僅限資料(建資料)
Views

Visual Studio 的ctr + 逗號沒有反應(設定小技巧)

 前言

  在看線上課程練習快捷鍵設定時,突然發現ctr + 逗號怎麼沒有反應?

  google後發現,參考保哥的文章找到相關設定方法,主要原因是原本

  win10的預設繁體中文版,預設輸入新注音,在visual studio中按下

  ctr + 逗號會造成其失效

 解決方式:歸納保哥的重點,在套用現在環境下,其實只有幾個步驟設定就 好了

 1.win10可以安裝新的English(United States)語言 (一開始找不到相關位置)
    按下開始>設定>時間與語言>語言>慣用語言(新增)
   

2.以上灌完,發現已經不用在後續設定,就可以正確使用了。

3.最後ENG輸入環境,語言切換快捷鍵是,Alt+Shift或Win+Space