我有以下JSON对象:
var algo, encrypt, nameOfRecord, configOutput = new Object();
algo = JSON.parse('{"algorithm" :"'+response.fieldConfigProperties.algorithmNames+'"}');
encrypt = JSON.parse('{"encryptionLevel" :"'+response.fieldConfigProperties.encryptionLevels+'"}');
nameOfRecord = JSON.parse('{"recordName" :"'+recordName+'"}');
configOutput = JSON.parse('{"outputConfiguration" :"'+outputConfiguration+'"}');
如何将所有这些一次传递给我的ajax jQuery?
这是我的jquery
$.ajax({
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json' },
url:"/FeildConfigurationServlet",
method:"POST",
data: //WHAT SHOULD I PASS HERE
dataType:"json",
success:function(response){
console.log(JSON.stringify(response));
console.log('success /FeildConfigurationServlet')
} })
感谢任何帮助。感谢:)
编辑:
这是我必须打电话的其他服务:
@RequestMapping(value = "/FeildConfigurationServlet", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
// writes a new field config details or update an already existing field
// configuration
protected @ResponseBody String feildConfigurationServlet(@RequestBody FieldConfigurationData fcd) {
String algorithm = fcd.getAlgorithm();
String encryptionLevel = fcd.getEncryptionLevel();
String fieldValueToBeConfigured = fcd.getFieldValueToBeConfigured();
String recordName = fcd.getRecordName();
String outputConfiguration = fcd.getOutputConfiguration();
String fileNameBigDataXML = CommonUtils.getConfigFolderPath()
+ "\\Config-XML\\tap-dap-anonymizer-configuration.xml";
String dataReadFieldOutputConfig = null;
String dataReadRecordStructure = null;
try {
dataReadFieldOutputConfig = readDataFromFile(
CommonUtils.getConfigFolderPath() + "\\Config-XML\\field_output_config.xml");
dataReadRecordStructure = readDataFromFile(
CommonUtils.getConfigFolderPath() + "\\Config-XML\\record_structure.xml");
} catch (IOException e) {
e.printStackTrace();
}
// read data from bigdata XML and from fieldconfig XML
String dataRead = null;
try {
dataRead = readDataFromFile(fileNameBigDataXML);
} catch (IOException e) {
e.printStackTrace();
}
// to append record sturcture(to get output under that fieldname)
dataRead = appendRecordStructureForRecordType(dataRead, dataReadRecordStructure, recordName,
fieldValueToBeConfigured);
// manipulations for the data
String finalDataForFieldOutputConfig = generateGenericFieldConfigStructure(dataReadFieldOutputConfig,
fieldValueToBeConfigured, outputConfiguration, algorithm, encryptionLevel);
// manipulated data whether to add or to update
try {
addOrUpdateFieldConfig(dataRead, fieldValueToBeConfigured, finalDataForFieldOutputConfig,
fileNameBigDataXML, recordName);
} catch (IOException e) {
e.printStackTrace();
}
// sending response
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("success", true);
return jsonObject.toString();
}
public static String appendRecordStructureForRecordType(String dataRead, String dataReadRecordStructure,
String recordName, String fieldValueToBeConfigured) {
dataReadRecordStructure = dataReadRecordStructure.replaceFirst("<dap-property-value></dap-property-value>",
"<dap-property-value>" + fieldValueToBeConfigured + "_hash</dap-property-value>");
String firstpart = dataRead.substring(0, dataRead.indexOf("<record-name>" + recordName + "</record-name>"));
String lastPart = dataRead.substring(dataRead.indexOf("<record-name>" + recordName + "</record-name>"));
String changeToAppend = lastPart.substring(0, lastPart.indexOf("</record-structure>")) + dataReadRecordStructure
+ lastPart.substring(lastPart.indexOf("</record-structure>"));
String finaldataRead = firstpart + changeToAppend;
return finaldataRead;
}
public static String generateGenericFieldConfigStructure(String dataReadFieldOutputConfig,
String fieldValueToBeConfigured, String outputConfiguration, String algorithm, String encryptionLevel) {
dataReadFieldOutputConfig = dataReadFieldOutputConfig.replaceFirst("<field-reference></field-reference>",
"<field-reference>" + fieldValueToBeConfigured + "</field-reference>");
dataReadFieldOutputConfig = dataReadFieldOutputConfig.replaceFirst(
"<drop-original-field></drop-original-field>",
"<drop-original-field>" + outputConfiguration + "</drop-original-field>");
dataReadFieldOutputConfig = dataReadFieldOutputConfig.replaceFirst("<algorithm></algorithm>",
"<algorithm>" + algorithm + "</algorithm>");
dataReadFieldOutputConfig = dataReadFieldOutputConfig.replaceFirst(
"<field-operation-name></field-operation-name>",
"<field-operation-name>" + fieldValueToBeConfigured + "_hash</field-operation-name>");
String firstData = dataReadFieldOutputConfig
.substring(dataReadFieldOutputConfig.lastIndexOf("<operation-configuration>"));
String data = dataReadFieldOutputConfig.substring(0,
dataReadFieldOutputConfig.lastIndexOf("<operation-configuration>") - 1);
firstData = firstData.replace("<algorithm></algorithm>", "<algorithm>" + encryptionLevel + "</algorithm>");
dataReadFieldOutputConfig = data + firstData;
return dataReadFieldOutputConfig;
}
public static void addOrUpdateFieldConfig(String dataRead, String fieldValueToBeConfigured,
String finalDataForFieldOutputConfig, String fileNameBigDataXML, String recordName) throws IOException {
if (dataRead.contains("<field-reference>" + fieldValueToBeConfigured + "</field-reference>")) {
// to be updated
int fieldReferenceIndex = dataRead
.indexOf("<field-reference>" + fieldValueToBeConfigured + "</field-reference>");
String firstPartdataRead = dataRead.substring(0, fieldReferenceIndex);
String remainingPartdataread = dataRead.substring(fieldReferenceIndex - 1);
// to be appended to remaining part
String inbet = StringUtils.substringBetween(finalDataForFieldOutputConfig, "<field-operations>",
"</field-operations>");
// first and last index of field <field-operations>
remainingPartdataread = remainingPartdataread.substring(0,
remainingPartdataread.indexOf("<field-operations>")) + "<field-operations>" + inbet
+ remainingPartdataread.substring(remainingPartdataread.indexOf("</field-operations>"));
dataRead = firstPartdataRead + remainingPartdataread;
writeToFile(dataRead, fileNameBigDataXML);
logger.debug("This is updating field config details : " + "writing field data to file : " + dataRead);
} else {
// for a new field config
int recordReferenceIndex = dataRead.indexOf("<record-reference>" + recordName + "</record-reference>");
String firstPartdataRead = dataRead.substring(0, recordReferenceIndex);
String remainingPartdataread = dataRead.substring(recordReferenceIndex - 1);
String LastPartBigData = remainingPartdataread.substring(0,
remainingPartdataread.indexOf("</record-anonymizer-configuration>")) + finalDataForFieldOutputConfig
+ remainingPartdataread
.substring(remainingPartdataread.indexOf("</record-anonymizer-configuration>") - 1);
dataRead = firstPartdataRead + LastPartBigData;
writeToFile(dataRead, fileNameBigDataXML);
logger.debug("This is writing new field config details : " + "writing field data to file : " + dataRead);
}
}
我必须将所有对象传递给此服务。我该怎么做?
更新: 我忘了添加这个课程
package com.entity;
public class FieldConfigurationData {
private String algorithm;
private String encryptionLevel;
private String fieldValueToBeConfigured;
private String recordName;
private String outputConfiguration;
public String getAlgorithm() {
return algorithm;
}
public void setAlgorithm(String algorithm) {
this.algorithm = algorithm;
}
public String getEncryptionLevel() {
return encryptionLevel;
}
public void setEncryptionLevel(String encryptionLevel) {
this.encryptionLevel = encryptionLevel;
}
public String getFieldValueToBeConfigured() {
return fieldValueToBeConfigured;
}
public void setFieldValueToBeConfigured(String fieldValueToBeConfigured) {
this.fieldValueToBeConfigured = fieldValueToBeConfigured;
}
public String getRecordName() {
return recordName;
}
public void setRecordName(String recordName) {
this.recordName = recordName;
}
public String getOutputConfiguration() {
return outputConfiguration;
}
public void setOutputConfiguration(String outputConfiguration) {
this.outputConfiguration = outputConfiguration;
}
@Override
public String toString() {
return "FeildConfigurationData [algorithm=" + algorithm + ", encryptionLevel=" + encryptionLevel
+ ", fieldValueToBeConfigured=" + fieldValueToBeConfigured + ", recordName=" + recordName
+ ", outputConfiguration=" + outputConfiguration + "]";
}
}
答案 0 :(得分:2)
您可以创建一个包含JSON对象数据的新对象
$.ajax({
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json' },
url:"/FeildConfigurationServlet",
method:"POST",
data: {
algo:algo,
encrypt:encrypt,
nameOfRecord:nameOfRecord,
configOutput:configOutput
},
dataType:"json",
success:function(response){
console.log(JSON.stringify(response));
console.log('success /FeildConfigurationServlet')
} })
答案 1 :(得分:0)
您可以向数据添加多个属性,因为您可以阅读here。所以你可以简单地使用data: {"algo": algo, "encrypt": encrypt ...}
答案 2 :(得分:0)
var algo, encrypt, nameOfRecord, configOutput = new Object();
var Obj = new Object();
algo = JSON.parse('{"algorithm" :"'+response.fieldConfigProperties.algorithmNames+'"}');
encrypt = JSON.parse('{"encryptionLevel" :"'+response.fieldConfigProperties.encryptionLevels+'"}');
nameOfRecord = JSON.parse('{"recordName" :"'+recordName+'"}');
configOutput = JSON.parse('{"outputConfiguration" :"'+outputConfiguration+'"}');
Obj = {algo, encrypt, nameOfRecord, configOutput}
希望这会有所帮助...
答案 3 :(得分:0)
首先不要手工构建JSON,特别是如果你打算将它解析成一个对象。
var data = {
algorithm :response.fieldConfigProperties.algorithmNames,
encryptionLevel: response.fieldConfigProperties.encryptionLevels,
recordName: recordName,
outputConfiguration: outputConfiguration,
fieldValueToBeConfigured: 'some value'
};
发送json时,你应该将内容类型设置为application / json,并将js数据作为json传递给
$.ajax({
url:"/FeildConfigurationServlet",
method:"POST",
data: JSON.stringify(data),
contentType: 'application/json',
dataType:"json",
success:function(response){
console.log(JSON.stringify(response));
console.log('success /FeildConfigurationServlet')
}
});