我有一个
jquery提交事件,它收集表单数据并将其放入jquery对象.我想获取该jquery对象并将其传递给coldfusion Web服务,我可以使用它来更新xml文件.我不希望Web服务的响应,我只想将其发送到Web服务并从那里调整数据.
@H_403_2@客户端/ JQuery:
在萤火虫中我得到“意想不到的性格” @H_403_2@只要问我,我会添加您需要的更多信息.
$("#update").on('submit',function() { $linkName = $('#update').find('#linkName').val(); $linkURL = $('#update').find('#linkURL').val(); $linkInfo = $('#update').find('#linkDesc').val(); $numOfLinks = $('.linkSection').length; if ($numOfLinks > 0){ // Here the sub link names and urls put into an array $subLinkName = []; $subLinkURL = []; $('.linkSection').each(function(index,element) { $subLinkName.push($(this).find('#subLinkName').attr('value')); $subLinkURL.push($(this).find('#subLinkURL').attr('value')); $data = {linkName: $linkName,linkURL: $linkURL,linkID : $linkID,linkDescription : $linkInfo,subLinkNames : $subLinkName,subLinkURLs : $subLinkURL}; }); // Optionally,you could put the name and url in the array object here but not sure which is better to do //$subLink =[]; //$('.linkSection').each(function(index,element) { //$subLink.push($(this).find('#subLinkName').attr('value')); //$subLink.push($(this).find('#subLinkURL').attr('value')); //}); }else{ alert('hey'); $data = {linkName: $linkName,linkDescription : $linkInfo}; } //alert($data); $.ajax({ type: "POST",data: { method: "UpdateRegularLink",returnFormat:"json",formData: JSON.stringify($data) },url: "../../WebServices/RMSI/rmsi.cfc",contentType: "application/json; charset=utf-8",dataType: "json",beforeSend: function() { alert('about to post'); },error: function(data,status,error){ alert(data+': '+status+': '+error); },done: function(data){ alert('success'); } }); });@H_403_2@服务器端/ CFC:
<cfcomponent> <cfset xmlpath = "e:\webapps\NRCNewsApps\RMSI\xml" /> <cffunction name="UpdateRegularLink" access="remote" output="false" > <cfargument name="formData" required="true" type="string" /> <cfset var cfStruct = DeserializeJSON(arguments.formData)> <!--- now I want to use the data ---> </cffunction> </cfcomponent>@H_403_2@在Chrome中我得到“未经授权”
在萤火虫中我得到“意想不到的性格” @H_403_2@只要问我,我会添加您需要的更多信息.
解决方法
因此,当您将要传递给coldfusion的数据进行字符串化时,coldfusion不会理解它并将各种字符添加到您的字符串中,这使得coldfusion无法读取.
@H_403_2@必须使用toString()作为中间方法调用,因为JSON数据包作为字节数组(二进制数据)出现,需要在ColdFusion将其解析为JSON值之前将其转换回字符串.
@H_403_2@也很好地调用@Chandan Kumar将方法添加到url的末尾,而不是将其传递给数据.我实际上一直在翻看这件作品,但最终它是如何起作用的,所以KUDOS给你
var ajaxResponse = $.ajax({ type: "POST",url: "../../WebServices/RMSI/rmsi.cfc?method=UpdateRegularLinkmethod=,data: JSON.stringify($data),//dataType: "json",beforeSend: function() { //alert($data); },error){ alert(data+': '+status+': '+error); } }).done(function(entry) { alert('success'); }); ajaxResponse.then( function( apiResponse ){ // Dump HTML to page for debugging. $( "#response" ).html( apiResponse ); } );@H_403_2@CFC
<cfcomponent> <cffunction name="UpdateRegularLink" access="remote" returntype="xml"> <cfset requestBody = toString( getHttpRequestData().content ) /> <!--- Double-check to make sure it's a JSON value. ---> <cfif isJSON( requestBody )> <!--- Echo back POST data. ---> <cfdump var="#deserializeJSON( requestBody )#" label="HTTP Body" /> </cfif> </cffunction> </cfcomponent>