我正在制作一项服务,点击即可将用户的Facebook信息保存到数据库中。现在的问题是我的服务被调用了两次。我无法弄清楚为什么它以这种方式表现。请帮助。
的index.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="css/jquery.jgrowl.min.css">
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript" src="scripts/jquery.validate.min.js"></script>
<script type="text/javascript" src="scripts/jquery.blockUI.js"></script>
<script type="text/javascript" src="scripts/jquery.jgrowl.min.js"></script>
<script type="text/javascript" src="scripts/fb.js"></script>
</head>
<body>
<div id="fb-root"></div>
<table align="center">
<tr>
<th>Registration Method</th>
</tr>
<tr>
<th><input type="button" value="Login With Facebook" id="fb-auth"> </th>
</tr>
</table>
</body>
</html>
fb.js
window.fbAsyncInit = function() {
FB.init({
appId : 'xxxxxxxxxxxxxxxx',
xfbml : true,
version : 'v2.1',
status : true,
cookie : true,
oauth : true
});
function updateButton(response) {
var button = document.getElementById('fb-auth');
if (response.authResponse) { // in case if we are logged in
var userInfo = document.getElementById('user-info');
FB.api('/me', function(response) {
var socialMediaRegistration={};
socialMediaRegistration['name']=response.name;
socialMediaRegistration['profileId']=response.id;
socialMediaRegistration['email']=response.email;
socialMediaRegistration['socialPlatform']="Facebook";
var mydata={};
mydata['socialMediaRegistration']=socialMediaRegistration;
submitData(mydata);
});
button.onclick = function() {
FB.logout(function(response) {
window.location.reload();
});
};
} else { // otherwise - dispay login button
button.onclick = function() {
FB.login(function(response) {
if (response.authResponse) {
window.location.reload();
}
}, {scope:'email,public_profile'});
}
}
}
// run once with current status and whenever the status changes
FB.getLoginStatus(updateButton);
FB.Event.subscribe('auth.statusChange', updateButton);
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "http://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol + 'http://connect.facebook.net/en_US/all.js';
if (document.getElementById('fb-root'))
{document.getElementById('fb-root').appendChild(e);};
}());
function submitData(data){
alert("this is below the submitdata");
alert(JSON.stringify(data));
$.support.cors = true;
$.jGrowl.defaults.position = 'center';
$.ajax({
type: "POST",
url: "http://localhost:8080/components/services/saveSocialMedia",//use ur service
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
crossDomain: true,
dataType: "json",
success: function (data, status, jqXHR) {
$.unblockUI();
$.blockUI({ message: null });
$.jGrowl('Application Reference: '+data["Id"]+' is created. <br/><br/>Have a nice day!', {
sticky: true,
header: 'Request Submitted Successfully!',
life: 5000,
speed: 'slow',
animateOpen: {
height: "show",
width: "show"
},
animateClose: {
height: "hide",
width: "show"
},
close: function(e,m,o) {
$.unblockUI();
//$("#ApplicationRequestForm")[0].reset();
},
click: function(msg) {
$.unblockUI();
//$("#ApplicationRequestForm")[0].reset();
$("div.jGrowl").jGrowl("close");
return true;
}
});
},
error: function (jqXHR, status) {
$.unblockUI();
$.blockUI({ message: null });
$.jGrowl('Please try again after sometime.', {
sticky: true,
header: 'Ouch! An Unexptected Error Occurred.',
life: 5000,
speed: 'slow',
animateOpen: {
height: "show",
width: "show"
},
animateClose: {
height: "hide",
width: "show"
},
close: function(e,m,o) {
$.unblockUI();
},
click: function(msg) {
$.unblockUI();
$("div.jGrowl").jGrowl("close");
return true;
}
});
console.log(jqXHR);
}
});
}
答案 0 :(得分:0)
当您使用CORS时,您的部分请求会获得preflighted。根据文件:
与简单请求(如上所述)不同,“预检”请求首先通过OPTIONS方法向另一个域上的资源发送HTTP请求,以确定实际请求是否可安全发送。跨站点请求是这样预检的,因为它们可能对用户数据有影响。特别是,如果出现以下情况,请求会被预检:
- 它使用GET,HEAD或POST以外的方法。此外,如果使用POST来发送具有除application / x-www-form-urlencoded,multipart / form-data或text / plain之外的Content-Type的请求数据,例如,如果POST请求使用application / xml或text / xml向服务器发送XML有效负载,则该请求将被预检。
- 它在请求中设置自定义标头(例如,请求使用标头,例如X-PINGOTHER)
您正在使用POST方法,但您的内容类型设置为application/json; charset=utf-8
,因此浏览器将首先使用OPTIONS
方法发送请求,然后将发送您的POST
。
并且,要让浏览器允许POST
,OPTIONS
响应必须包含某些标题:
Access-Control-Allow-Origin: http://foo.example
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Headers: X-PINGOTHER
Access-Control-Max-Age: 1728000
这些告诉您的浏览器http://foo.example
是允许的来源,POST
,GET
和OPTIONS
是允许的方法,X-PINGOTHER
是一个允许的标题。
因此,您的服务被调用两次,因为它会被浏览器自动预检。