[补充说:]谢谢各位回复。我知道ajax必须在服务器上使用。我想知道在这个例子中是否有办法将本地json文件中的数据加载到js中?
我有一个名为public class LoginActivity extends AbsRuntimePermission {
private static final int REQUEST_Permission =10;
private TextView info;
private LoginButton loginButton;
private CallbackManager callbackManager;
private SparseIntArray mErrorString;
private Button facebook_button;
ProgressDialog progress;
private String facebook_id,f_name,m_name,l_name,gender,profile_image,full_name,email_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//for permission in manifest file cause it is launch activity
requestAppPermissions(new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.CALL_PHONE,
Manifest.permission.INTERNET},R.string.msg,REQUEST_Permission);
//facebook
info = (TextView) findViewById(R.id.info);
loginButton = (LoginButton) findViewById(R.id.login_button);
progress = new ProgressDialog(LoginActivity.this);
progress.setMessage(getResources().getString(R.string.please_wait_facebooklogin));
progress.setIndeterminate(false);
progress.setCancelable(false);
facebook_id=f_name=m_name=l_name=gender=profile_image=full_name=email_id="";
//facebook sdk
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
//register callback object for facebook result
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
progress.show();
Profile profile = Profile.getCurrentProfile();
if(profile != null){
facebook_id = profile.getId();
f_name=profile.getFirstName();
m_name=profile.getMiddleName();
l_name=profile.getLastName();
profile_image=profile.getProfilePictureUri(400,400).toString();
}
//show Toast
GraphRequest request =GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback(){
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
email_id = object.getString("email");
gender = object.getString("gender");
String Profile_name = object.getString("name");
long fb_id = object.getLong("id"); //use this for logout
//Starting a new activity using this information
Intent i = new Intent(LoginActivity.this, MainActivity.class);
i.putExtra("type", "facebook");
i.putExtra("Facebook_id", facebook_id);
i.putExtra("f_name", f_name);
i.putExtra("m_name", m_name);
i.putExtra("l_name", l_name);
i.putExtra("full_name", full_name);
i.putExtra("Profile_image", profile_image);
i.putExtra("email_id", email_id);
i.putExtra("gender", gender);
progress.dismiss();
startActivity(i);
finish();
}catch (JSONException e){
e.printStackTrace();
}
}
});
request.executeAsync();
}
@Override
public void onCancel() {
Toast.makeText(LoginActivity.this,getResources().getString(R.string.login_canceled_facebooklogin),Toast.LENGTH_SHORT).show();
progress.dismiss();
}
@Override
public void onError(FacebookException error) {
Toast.makeText(LoginActivity.this,getResources().getString(R.string.login_failed_facebooklogin),Toast.LENGTH_SHORT).show();
progress.dismiss();
}
});
//facebook button click
facebook_button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this, Arrays.asList("public_profile","user_friends","email"));
}
});
}
private void logoutFromFacebook(){
try{
if(AccessToken.getCurrentAccessToken()==null){
return;
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode,resultCode,data);
}
@Override
public void onPermissionGranted(int requestCode) {
//anything after permission Granted
Toast.makeText(getApplicationContext(),"Permission granted",Toast.LENGTH_LONG).show();
}
public void goTo(View view) {
Intent intent = new Intent(this, MainActivity.class);
this.startActivity(intent);
}
的文件。我尝试将此JSON数据加载到JavaScript中。我在桌面上保存了.json和.html文件但是当我点击HTML时,没有显示任何内容。我尝试在employee.json
方法中使用alert()
,但它不起作用。我不确定我的AJAX方法有什么问题。我需要做什么才能从JSON中获取.ajax()
并将其放入javaScript中的数组中?提前谢谢。
id
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
var res = [];
$.ajax({
url: 'employee.json',
dataType: 'json',
method: 'get',
cache: false,
success: function(data) {
$(data.employee).each(function(index, value) {
res.push(value.id);
});
document.getElementById("demo").innerHTML = res.toString();
}
});
</script>
</body>
</html>
答案 0 :(得分:0)
作为替代方案,您可以将JSON作为javascript加载,只需将所有JSON数据包装到
中 var employees = { /*all your json data here*/ }
并将其另存为employees.js
然后你可以在html中使用常规脚本标记:
<script src="/yourpath/employees.js"/>
然后在你的javascript&#34;员工&#34;将是一个全球性的对象。 所以你将拥有:
employess.employee[1].FirstName
结果&#34; bryant&#34;在你的情况下;