I have AJAX writing a result to <span id="response"></span>
, which displays as expected. But, I need to convert to a CF variable so I can write the content to the database. Using
<cfsavecontent variable="JSONResponse"><span id="response"></span></cfsavecontent>
allows me to display the new JSONResponse variable on the page correctly, but when I take the same varible and write it to the database, it writes the <span id="response"></span>
tag into the table - not the actual content. Does anyone have any suggestions? Thanks
Asked
Active
Viewed 127 times
-1

Jeremy Kwee
- 39
- 5
-
What is the value of the JSONResponse variable? Based on what you said in your question, `
`, my expected value is, ``. – Dan Bracuk Apr 16 '20 at 13:29 -
Comment from GSR - Do you want to write only the JSOn response and not the HTML, can you clarify your question, What exactly you are trying to do here? – Dan Bracuk Apr 16 '20 at 14:23
-
Thanks - the value being passed to the DIV and displayed on the page is a JSON string. I want to take that string and write it to the database. I can't find a way of converting the AJAX variable into a CF variable any other way. I was hoping that this would work, as it is displaying the json string on the page correctly if I simply do a cfoutput around the cfsavecontent created #JSONResponse# variable. – Jeremy Kwee just now – Jeremy Kwee Apr 16 '20 at 14:26
-
Does your ajax call go to a ColdFusion page? – Dan Bracuk Apr 16 '20 at 14:29
-
Yes, the AJAX is being called and the result is displayed in a DIV on a .cfm page. – Jeremy Kwee Apr 16 '20 at 14:38
-
Do you want to write only the JSOn response and not the HTML, can you clarify your question, What exactly you are trying to do here – Notion Apr 16 '20 at 13:52
-
Thanks - the value being passed to the DIV and displayed on the page is a JSON string. I want to take that string and write it to the database. I can't find a way of converting the AJAX variable into a CF variable any other way. I was hoping that this would work, as it is displaying the json string on the page correctly if I simply do a cfoutput around the cfsavecontent created #JSONResponse# variable. – Jeremy Kwee Apr 16 '20 at 14:26
-
Can you show some kind of dummy fiddle which can give exact idea as to how it should loo like and how it is now. @JeremyKwee – Notion Apr 16 '20 at 15:44
-
@DanBracuk Thanks, Even you have already Specified the Option of using `CHAR`, but Thanks for letting me add my Answer which is exactly the same what you specified, `CHAR` will solve his problem – Notion Apr 16 '20 at 18:26
1 Answers
1
Thanks @GSR & @Dan - I managed to work out a solution by forwarding on to a CFM page that writes to the DB, via another nested ajax post, based upon the response:
var postData = {username: "user@company.com", password: "Ruu3992032!883jj22uje"};
var ajaxResponse = $.ajax({
type: "post",
url: "https://api.company.com/v1/authenticate",
contentType: "application/json",
data: JSON.stringify( postData )
})
// When the response comes back, forward on to another cfm page with insert statement.
ajaxResponse.then(
function( apiResponse ){
$.ajax({
type: "post",
url: "WriteToDB.cfm",
data: jQuery.param({ payload: JSON.stringify( apiResponse ) }) ,
contentType: 'application/x-www-form-urlencoded; charset=UTF-8'
})
}
);

Jeremy Kwee
- 39
- 5
-
Is it my imagination or are you stringifying the original string twice? – Dan Bracuk Apr 16 '20 at 18:35