Home  >  Article  >  Web Front-end  >  Detailed explanation of steps to read properties using ajax

Detailed explanation of steps to read properties using ajax

php中世界最好的语言
php中世界最好的语言Original
2018-04-25 15:09:182073browse

This time I will bring you a detailed explanation of the steps for ajax to read properties. What are the precautions for ajax to read properties? The following is a practical case, let's take a look.

The contents of the properties resource file are as follows:

hello=englishww
name=english zk
emailEmpty=Field cannot be empty!
emailInvalid=Invalid email address!

js calls ajax processing code:

$.ajax({
 type:'POST',
 dataType:'json',
 url:'/jeecms/jeecms/ajax/cms/getResourceBundle.do',
 async:false,
 success:function(data){
  jsonData=data.jsI18n;//jsI18n是java返回时赋予的名称
  jsi18n=eval_r('('+jsonData+')');//转化为json对象
  alert("property is "+jsi18n.hello);
 },
 error:function(data){
  alert("error");
 }
});

javaProcessing file getResourceBundle.do code:

publicString getResourceBundle(){
 ResourceBundle RESOURCE_BUNDLE;
  if(contextPvd.getSessionAttr("gLanguage")!=null&&contextPvd.getSessionAttr("gLanguage").equals("1")){ 
 RESOURCE_BUNDLE=ResourceBundle.getBundle("jsI18n",Locale.ENGLISH);
}else{
 RESOURCE_BUNDLE =ResourceBundle.getBundle("jsI18n",Locale.CHINA);
}//判断语言类别的,忽视
Set keySet=RESOURCE_BUNDLE.keySet();
//读取资源文件数据拼接成json格式字符串返回
String jsonString = newString();
jsonString+="{";
for(String key:keySet){
 jsonString+='"'+key+'"'+":"+'"'+RESOURCE_BUNDLE.getString(key)+'"'+",";
}
//把字符串赋给返回对象的jsI18n(这里随意)
jsonRoot.put("jsI18n",jsonString.substring(0,jsonString.length()-1)+"}");
return SUCCESS;
}

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to implement partial refresh function with jQuery and ajax

How to use Ajax to implement loop

The above is the detailed content of Detailed explanation of steps to read properties using ajax. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn