顯示具有 ajax 標籤的文章。 顯示所有文章
顯示具有 ajax 標籤的文章。 顯示所有文章

2017年5月31日 星期三

JavaScript學習筆記-cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https

cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https

今天練習js操作ajax的時候一直無法取得來源網頁資訊,透過開發工具(f12)查看,發現了這個錯誤訊息! 找了一下,發現只要在chrome目標上加上『 –allow-file-access-from-files』即可。

2017年5月10日 星期三

JavaScript學習筆記-AJAX-JSON傳遞

var dd ={
  mail:'ddd@gmail.com',
  password:'1234'
}
//建立一個物件
var ajx = new XMLHttpRequest();
ajx.open('post','https://傳遞的網址',true);
ajx.setRequestHeader('content-type','application/json');
//設定格式為json
var cc=JSON.stringify(dd);
//將物件轉字串
ajx.send(cc);

2017年5月9日 星期二

javascript-學習筆記-AJAX-XMLHttpRequest

操作步驟

var ajx = new XMLHttpRequest();
//readyState=0

ajx.open('1','2',3);
//readyState=1

ajx.send(null);
//如果是get沒有送參數的話,可以直接給null

ajx.onload = function(){
//在讀取完成之後才執行;
    ajx.responseText;
    var str = ajx.JSON.parse(ajx.response.Text);
    //透過JSON.parse將取得字串轉陣列
}

ajx.status==200;
//可用來判斷狀態是否正常

open參數

  • 1-格式,get、post
  • 2-要取值的網址
  • 3-同步false(資料回傳才會繼續執行)/非同步true(非同步,不待資料回傳就繼續執行了)

readyState

  • 0-已產生XMLHttpRequest
  • 1-已OPEN,未傳送資料
  • 2-已傳送
  • 3-LOADING
  • 4-已取得資料