[JS]Object and String in ajax function

2016-11-2 写技术

The difference between json object and json string while post with ajax:


var login = function(){
	var user = $("input[name=username]").val();
	var password = $("input[name=password]").val();
	
	var data = {
		username:user,
		password:password
	};
	data = JSON.stringify(data);
	console.log(data);
	$.post(url_login, data, function(ele, status){
		console.log(ele);
		console.log(status);
	});
}

Attention the command:

"data = JSON.stringify(data)"

It will post the data as normal request values if you didn't change data into a string in the function post. Otherwise as input stream.

标签: JS

发表评论:

Powered by anycle 湘ICP备15001973号-1