Lucee ColdFusion.Ajax.submitForm - coldfusion

I'm converting a site from ColdFusion to Lucee (for the first time). In ColdFusion, after using the cfajaximport tag, I can run JS code similar to this:
ColdFusion.Ajax.submitForm('runMe', 'runCode.cfm', callback_testMe, errorHandler_testMe);
I seem to be unable to run this in Lucee. I'm looking for some kind of Lucee alternative, but can't seem to find anything.
Basically, I'm wanting to submit form data, run some server side stuff, then return the results without refreshing the page.
Any help would be greatly appreciated.

Lucee does not have CF UI tags, you're going to have to do this with jQuery AJAX or similar.
var formID = "runMe"; // formId Param
$("#"+formID).on("submit", function (e) {
e.preventDefault(); // Prevent page load
$.ajax({
type: "POST", // httpMethod Param
url: "runCode.cfm", // URL Param
data: $(this).serialize(),
success: function (data) {
console.log(data); // callbackhandler
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(thrownError); // errorHandler
}
});
})
This should do exactly the same thing and I even pointed out the similar params from ColdFusion.Ajax.submitForm

Related

How to get the Rendering Datasource after an AJAX postback

I have the ajax code below:
$.ajax({
url: 'api/sitecore/SmartAds/GetSmartAddsItem',
type: "GET",
dataType: "json",
success: function (result) {
alert(result);
}
});
The problem is the Sitecore.Context.Item.ContextRendering is null in the ajax call.
Question: How do I get the rendering datasource during AJAX call?
I found the below code here, but I am having trouble getting it to work:
public class FormModelBinderProvider : Dictionary, IModelBinderProvider
{
public IModelBinder GetBinder(Type modelType)
{
var binders = from binder in this
where binder.Key.IsAssignableFrom(modelType)
select binder.Value;
return binders.FirstOrDefault();
}
}
I am getting an error that says that Dictionary requires 2 type arguments.
Any help is appreciated.
The first issue with your code is that the AJAX request you are making is a 'GET' request rather than a post, so it won't be passing in the hidden field value for the datasource item. To clarify, the article covers situations where a form is posted back, not AJAX GET requests.

Getting callback is undefined error in firebug console while creating a datasource using kendo ui

My webservice generates jsonp response same as http://demos.kendoui.com/service/products.
When i try to create datasource for my webservice i am getting callback is not defined error in firebug console.
Webservice response.
callback([{"category":null,"productName":"Puma","productId":1,"quantity":0,"price":3000.0,"categoryId":1,"description":"ok"}])
But when i use kendo ui webservice (http://demos.kendoui.com/service/Products) i am getting a valid datasource.
Code :
$(document).ready(function() {
var dataSource = new kendo.data.DataSource({
transport: {
read: {
//url: "http://demos.kendoui.com/service/products",
url: "http://localhost:8080/mobile-services/rest/categories/1/products.json",
dataType: "jsonp"
}
},
pageSize: 12
});
$("#pager").kendoPager({
dataSource: dataSource
});
$("#listView").kendoListView({
dataSource: dataSource,
template: kendo.template($("#template").html())
});
});
please suggest.
I guess the function name is not callback callback is just the key and the function itself maybe called sth like jQuery17101014779508113 Can you look at what your datasource is sending to the server during the read operation? (Chrome / Firebug Network Tab) I'm taking the $_REQUEST['callback'] variable (with a PHP-Script) and just returning it as the padding. Since you're just using a static .json file and the callback function name will be changed on each request (to prevent caching), you won't ever have the correct function name.
So: I suggest you use JSON instead of P or you dynamically return the same callback you're receiving.
Cheers....
You have specified JSONP as dataType and you are requesting a JSON file. JSONP and JSON are not the same. Try using dataType: "json".

ajax - jquery - xml - webservice

I am trying to access the method GetWeatherByPlaceName from the service http://www.webservicex.net/weatherforecast.asmx. In my "data" attribute included city name as "newyork" but it is not showing any result. Please let me know if it is correct way of representing method name in the url.
$(document).ready(function(){
$.ajax({
type:"POST",
url:"www.webservicex.net/weatherforecast.asmx/GetWeatherByPlaceName",
data:"newyork",
contentType:"text/xml; charset=utf-8",
dataType:"xml",
success:function(msg){
$('span').html(msg);
}
});
});
I'm not sure if it will help, but the service should either receive an url encoded parameters string or a soap formated message.
So, i think you should replace your call by
$(document).ready(function(){
$.ajax({
type:"POST",
url:"www.webservicex.net/weatherforecast.asmx/GetWeatherByPlaceName",
data:"PlaceName=newyork",
dataType:"xml",
success:function(msg){
$('span').html(msg);
}
});
});

Display JSON response text from ajax in jQuery Template

I need to pass the response from ajax call to a jquery template.The response json is not malformed.I have checked this by using alert statements in the ajax fn.When the response is passed to the template,it does not get recognized.For example,when I use ${field1} in template,nothing gets displayed in the browser.No error messages are displayed at the browser.Can someone help me fix this issue?
Json response from server:
{
"field1": 23432434,
"field2": "sometext",
}
Ajax fn:
function getinfo(uri)
{
jQuery.ajax({
url: 'http://{{request.META.SERVER_NAME}}'+uri,
success: function(info) {
return info;
},
async: false,
dataType: 'jsonp'
});
}
Template:
<script id="infoTemplate" type="text/x-jQuery-tmpl">
<div>${field1}</div>
</script>
Code to Bind JSON to template:
<script id="Template1" type="text/x-jQuery-tmpl">
{{tmpl(getinfo(uri)) "#infoTemplate"}}
</script>
Note: I can't use the following method to bind JSON with template.That's a long story.
function getinfo(uri)
{
$.getJSON('http://{{request.META.SERVER_NAME}}'+uri, function(data) {
$("#infoTemplate").tmpl(data).appendTo("#somedivid");
});
}
That's not how callbacks work. You're returning info to the callback function, and not to getinfo.
You either have to do something like you proposed after, or keep the result from the ajax call in a global var and call the tmpl function after while, to be sure that you have already got the answer from the ajax call. The first way is the way to go.

elusive jQuery JSON Parser error

I have a an ajax call returning JSON to a loop to populate form fields. The raw data looks fine, but it never makes it though the loop and into the form, and an "Uncaught SyntaxError: Unexpected token :" error is thrown at line 1 of the document.
This is the ajax function:
$(function() {
$('#id_license' ).blur( function() {
q = $( '#id_license' ).val();
$.ajax({
datatype: 'json',
type: "POST",
data: 'lic='+q,
url: "/usr/xhr_license_search/?xhr",
success: function(data)
{ $.each(data, function(field, value){
$("#id_"+field).val(value);
});
console.log(data);
}
});
});
});
This is the raw JSON returned:
(manually anonymized)
{"First_Name":"Jon","Last_Name":"Doe","address":"2 A st.","city":"anthtown","grade":"T5","licNum":"08933","state":"TA","user":1099,"zipCode":09117}
I'm using json.dump() in Django to format the response.
Thank you in advance for your incites.
I've had this problem quite a few times before, it's something to do with the return dataType. By using $.get or $.post instead of $.ajax I've been able to solve the problem, so for you:
$.post('/usr/xhr_license_search/?xhr', 'lic='+q, function(data){
$.each(data, function(field, value){
$("#id_"+field).val(value);
});
}, 'json');
Doing that should give you no error.
I'm pretty sure JSON will be confused by the leading 0 in the zipCode; that shouldn't parse as a number, according to json.org . Are you sure you didn't intend that to be a string?
Try adding a pair of parentheses around the raw json, which makes it like
({"First_Name":"Jon","Last_Name":"Doe","address":"2 A st.","city":"anthtown","grade":"T5","licNum":"08933","state":"TA","user":1099,"zipCode":09117})