WebサービスへのAJAX呼び出しを使用してサービス呼び出しのリストを取得しています。Webサービスから返された各サービス呼び出しを繰り返し処理し、SQLiteテーブルに挿入する必要があります。ループのタイミングに苦労しています。たとえば、2つのサービス呼び出しがある場合、2つの行がテーブルに挿入されますが、両方とも2番目の呼び出しからのデータになります。変数宣言と値の割り当てを移動してみましたが、それらがトランザクション内にある場合は、すべてのフィールドが未定義で返されます。トランザクションの外部では、すべての行が最後のレコードとして挿入されます。
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "MyService.asmx/GetCalls",
data: '{ user:"' + user +'"}',
success: function (data) {
saveCalls(data.d);
},
error: function (xhr) {
var err = eval("(" + xhr.responseText + ")");
alert("Retrieve Calls: " + err.Message);
}
});
function saveCalls(result) {
var insertString = "Insert Into Calls Values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
var call, po, adrsCode, status, dispDate, dispTime, desc, customer, address, city, state, zip, phone, equipment, tracking, srvType, statDesc;
$.each(result, function () {
call = this.CallNumber;
po = this.PoNumber;
adrsCode = this.AdrsCode;
status = this.Status;
dispDate = this.DispatchedDate;
dispTime = this.DispatchedTime;
desc = this.Description;
customer = this.Customer;
address = this.Address;
city = this.City;
state = this.State;
zip = this.Zip;
phone = this.Phone;
equipment = this.Equipment;
tracking = this.Tracking;
srvType = this.SrvType;
statDesc = this.StatDesc;
db.transaction(function (tx) {
tx.executeSql(insertString, [call, po, adrsCode, status, dispDate, dispTime, desc, customer, address,
city, state, zip, phone, equipment, tracking, srvType, statDesc],
onSqlSuccess, onSqlError);
});
});
}
編集:同じ問題を抱えている人のために働いたコード:
function saveCalls(result) {
var insertString = "Insert Into Calls(CallNumber, PoNumber, AdrsCode, SrvStat, DispatchDate, DispatchTime, SvcDescription, CustNmbr, Address, "
+ "City, State, Zip, Phone, Equipment, Tracking, SrvType, StatDesc) ";
$.each(result, function () {
insertString = insertString + "Select '{0}' as CallNumber,'{1}' as PoNumber,'{2}' as AdrsCode,'{3}' as SrvStat,'{4}' as DispatchDate,'{5}' as DispatchTime,'{6}' as SvcDescription,'{7}' as CustNmbr,'{8}' as Address,'{9}' as City,'{10}' as State,'{11}' as Zip,'{12}' as Phone,'{13}' as Equipment,'{14}' as Tracking,'{15}' as SrvType,'{16}' as StatDesc Union "
.format(this.CallNumber, this.PoNumber, this.AdrsCode, this.Status, this.DispatchedDate, this.DispatchedTime,
this.Description, this.Customer, this.Address, this.City, this.State, this.Zip, this.Phone, this.Equipment,
this.Tracking, this.SrvType, this.StatDesc);
});
db.transaction(function(tx) {
tx.executeSql(insertString.substring(0, insertString.length - 6), [],
onSqlSuccess, onSqlError);
});
}