1. 程式人生 > >關於RESTFul架構Api介面的跨域問題

關於RESTFul架構Api介面的跨域問題

在註明允許跨域後,get、post請求方式均可使用,但是put、delete請求方式還是不能使用。

// 跨域請求
header("Access-Control-Allow-Origin: *");

所以採用了以下方法,在ajax中使用post方法偽裝請求方式,通過data傳值,傳遞_method變數,傳遞需要真正使用的請求方式。

	var urlstr='http://172.16.0.188/news/2';
	$.ajax({
		type:'post',
		async:true,
		url:urlstr,
		data:{
			_method:'delete'
		},
		success:function(response){
			console.log(JSON.stringify(response));
		}
	})