JQuery return code - ajax

Got a problem with jquery. I cant get a .xml file from the server, using the ajax function in jquery. I tried to call an alert upon succes and upon error, but nothing triggers. I did it this way:
$.ajax({
type: "GET",
url: "Levels.xml",
dataType: "xml",
error: function(){
alert("Ajax failed!");
},
success: function(xml){
alert(xml);
}
});
That did not work.
Edit: I have this code in my xml file:
<?xmlversion="1.0" encoding="ISO-8859-1"?>
<Level id="TestLevel" theme="woods">
<Wall>
<Row>111111111111111111111</Row>
<Row>100000000000000000001</Row>
<Row>100000000000000000001</Row>
<Row>100000000000000000001</Row>
<Row>100000000000000000001</Row>
<Row>100000000000000000001</Row>
<Row>100000000000000000001</Row>
<Row>100000000000000000001</Row>
<Row>100000000000000000001</Row>
<Row>100000000000000000001</Row>
<Row>100000000000000000001</Row>
<Row>111111111111111111111</Row>
</Wall>
</Level>

Renaming the XML File to something else like "sites.xml" fixes this on my server.
Appears that "levels" is somehow related to the xml request by ajax.
cheers
PrimuS

Related

Posting form using AJAX

can anyone please help me with sending the below form using Ajax. All I want is to send it to the trolley1.php page for processing, no call backs or anything like that. Basically replicate the form but sending it with Ajax so the page does not go to the trolley1.php page. I have tried so many methods but have not been able to do this. Bill Gates or Steve Wozniak if you guys are reading this, please help
This gives me a console $.Ajax is not a function in the console
<script>
$(document).ready(function(){
$('form').submit(function(event){
event.preventDefault();
var form_data = $(this).serialize();
$.ajax({
url: "trolley1.php",
type: "POST",
dataType:"json",
data: form_data
}).done(function(data){
alert("Item added to Cart!");
}
});
});
</script>
<?php
echo "
<div class='col-sm-3 mt-5'>
<form class='ajax' method='post' action='trolley1.php?action=add&id=$id'>
<div class='products'>
<a>$img</a>
<input type='hidden' name='id' value='$id'/>
<input type='hidden' name='name' value='$product'/>
<input type='hidden' name='price' value='$price'/>
<input type='text' name='quantity' class='form-control' value='1'/>
<input type='submit' name='submit' style='margin-top:5px;' class='btn btn-info'
value='Add to Cart'/>
</div>
</form>
You have one syntax error in your JS Code - see correct code
$(document).ready(function(){
$('form').submit(function(event){
event.preventDefault();
var form_data = $(this).serialize();
$.ajax({
url: "trolley1.php",
type: "POST",
dataType:"json",
data: form_data
}).done(function(data){
alert("Item added to Cart!");
});
});
});
And you are using jQuery as additional javascript libary. jQuery uses $ to access the methods (e.g. $.ajax) Thats the reason why you get undefined as error.
So you need to load the libary first at the beginning of your page (inside <head>). E.g. directly from their CDN
<script src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
Then it should work for you

Cordova $.ajax request wont work

I am having issue with cordova ajax reuest.
$.ajax({
url: syncURL,
data: (changed ? {start: changed + 1} : ''),
dataType: "json",
success: function (data) {
console.log('Ajax success');
callback(data);
},
error: function(model, response) {
console.log('Ajax error');
//app.notify(response.responseText, 'error');
callback(null);
}
});
The program shows Ajax success in console log. I can see the content inside app while watching it on the browser with:
cordova serve
Unfortunately if i want to deploy to ios simulator or on android device, the app wont load the content and get an empty json with no data.
What i am doing wrong. The simulator or android device got network connection.
I dont see nothing in log too.
Also added
cordova plugins add cordova-plugin-whitelist
cordova version is 7.1.0
I bumped into same error starting with phonegap-vue-f7 template and using axios as Ajax library.
For same request to http://my.domain.com/api/request :
returns code 200 with local cordova serve (npm start with phonegap-vue-f7 template)
returns code 404 when I deployed to real device
The phonegap-vue-f7 template furnish a pre-filled config.xml with this:
<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
at this point I had to install cordova-plugin-whitelist to make it work.
So the cordova-plugin-whitelist plugin correctly configured seem to be the answer.
not sure, but may be the data: value is not correct
data: (changed ? {start: changed + 1} : '')
What if you change to:
data: (changed ? '{start: changed + 1}' : '')

ajax call to do action when onclick checkbox in ofbiz

I want to use checkboxes with options say id1,id2.when i choose id1 a ajax will run through javascript and call the appropriate action and return the response.i dont want to reload the page.please post some freemarker, controller.xml, java codes to do this.
Based on my understanding of the question, the following script might be helpful you:
in ftl file itself/ in seperate js file you add the following script:
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#id1").click(function(){
if(jQuery(this).is(":checked")) {
var reqUrl= "checkbox1_Url";
sendAjaxRequest(reqUrl);
}
});
jQuery("#id2").click(function(){
if(jQuery(this).is(":checked")) {
var reqUrl= "checkbox2_Url";
sendAjaxRequest(reqUrl);
}
});
});
function sendAjaxRequest(reqUrl){
jQuery.ajax({
url : reqUrl,
type : 'POST',
data : {'var1': 'val1', 'var2' : 'val2'}, //here you can pass the parameters to
//the request if any.
success : function(data){
//You handle the response here like displaying in required div etc.
},
error : function(errorData){
alert("Some error occurred while processing the request");
}
});
}
</script>
In freemarker,
<input type="checkbox" id="id1" name="checkbox1" />
<input type="checkbox" id="id2" name="checkbox2" />
In controller.xml:
<request uri="checkbox1_Url">
<!-- fire if any events are here -->
<response name="success" type="view" value="ViewName1" />
<response name="error" type="view" value="errorViewName" />
</request>
<request uri="checkbox2_Url">
<!-- fire if any events are here -->
<response name="success" type="view" value="ViewName2" />
<response name="error" type="view" value="errorViewName" />
</request>
<view-map name="ViewName1" type="screen" page="component://.../widget/screensFileName#screenName"/>
<view-map name="ViewName2" type="screen" page="component://.../widget/screensFileName#screenName"/>
You define two screens in widgets as specified in the above path(page="...." attribute of view-map tag).
jQuery(function() {
jQuery('input[type = checkbox]').bind('click',function() {
var categoryId ="";
if(jQuery(this).is(':checked')) {
categoryId = jQuery(this).val();
}
var reqUrl= "categoryurl";
sendAjaxRequest(reqUrl, categoryId);
});
});
function sendAjaxRequest(reqUrl, categoryId){
jQuery.ajax({
url : reqUrl, // Here the Url will have to return the all products related to
//passed category
type : 'POST',
data : {'categoryId': categoryId}, //here you can pass the parameters to
//the request if any.
success : function(data){
//You handle the response here display all products in a div etc.
},
error : function(errorData){
alert("Some error occurred while processing the request");
}
});
}
<input type="checkbox" id="id1" name="checkbox1" value="yandamuri_cat"/><label for="id1">YandaMuri</label>
<input type="checkbox" id="id2" name="checkbox2" value="chetanBhagat_cat"/><label for="id2">Chetan Bhagath</label>
In Controller.xml:
<request uri="categoryurl">
<!-- fire if any events are here -->
<response name="success" type="view" value="ViewName" />
<response name="error" type="view" value="errorViewName" />
</request>
<view-map name="ViewName" type="screen" page="component://.../widget/screensFileName#screenName"/>
Define a screen in appropriate location/path specified in above page attribute of viw-map tag.
Here, the screen may be without decorator, so that the response will come without decorator and so you can display them in the current page, without reloading. Entire page.
I guess this might help you.

Barcode scanner and jquery ajax call?

I am stuck with the following: I have a simple form on index.phpwith one input field. On button click the form makes a jquery ajax request to lookup.php which returns some data. This data is shown on index.php. So far nothing special and it works when i use the keyboard.
Now when i use the barcode scanner it doesn't work, with some trial and error it looks like the barcode scanner uses the "submit" handler. But in my case the ajax request is handled by
$("#btnSubmit").on("click",
and button btnSubmit is of the type=button and not submit.
How can i fix this so the barcode scanner is also making the ajax request?
jquery submit with $.ajax doesn't seem to work
If you have a simple form like:
<div id="status"></div>
<form action="">
<input id="foo"/>
<input type="submit" value="Submit">
</form>
You can do:
$("form").submit(function(e) {
e.preventDefault();
$.ajax({
url: "lookup.php",
type: "POST", // Can change this to get if required
data: { foo: $("#foo").val(); },
success: function(data) {
$("#status").html(data);
},
error: function(jqXHR, textStatus, errorThrown) {
$("#status").text(textStatus);
}
});
});

Spring.addElementDecoration for dijit.form.filteringselect fails in IE

I am using Spring Roo to create a page that makes ajax call and populates a section of the page with the search form from the Ajax call. This is all done by using standard Roo tags and Javascript libraries. The form that is returned by Ajax is simple, just one select list.
<jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields"
xmlns:form="urn:jsptagdir:/WEB-INF/tags/form"
xmlns:springform="http://www.springframework.org/tags/form"
xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:spring="http://www.springframework.org/tags" version="2.0">
<div>
<jsp:directive.page contentType="text/html;charset=UTF-8" />
<jsp:output omit-xml-declaration="yes" />
<form:search
id="fc_domain_profile_combination"
modelAttribute="combination" path="/profiles/search/combinations"
z="e2lXrqKKD2CS7Zjefas2ZXPW4xYNM=">
<field:select field="searchProcessingCenter" renderempty="true" disabled="true"
id="c_profile_combofilter_processingcenter"
items="${proccenters}" path="/profiles"
itemLabel="name" itemValue="id"/>
</form:search>
<input type="hidden" id="blockSubmitHd" value="false" />
</div>
This code works fine in Firefox and Google Chrome but fails in IE with "Unknown error". The problem is happens in the Ajax method included in Roo:
function callXhrGet(contentId, ajaxUrl){
var _targetNode = dojo.byId(contentId);
var xhrArgs = {
url: ajaxUrl,
preventCache: true,
handleAs:"text",
load: function(data) {
dojo.html.set(_targetNode,data,{parseContent:true});
dojo.forEach(dojo.query("script", _targetNode),function(node){
dojo.eval(node.innerHTML);
});
dojo.parser.parse(_targetNode);
},
error: function(error) {
_targetNode.innerHTML = "An unexpected error occurred: " + error;
}
};
dojo.xhrGet(xhrArgs);
}
The dojo.eval(node.innerHTML); fails when tries to process following line:
Spring.addDecoration(new Spring.ElementDecoration({elementId : '_searchProcessingCenter_id', widgetType: 'dijit.form.FilteringSelect', widgetAttrs : {hasDownArrow : true, required : false, invalidMessage: 'Please enter valid Role Type' }}));
The interesting part is that when the list is empty it works fine, other dijit.form.tags work fine as well. Any advice and feedback how to resolve this issue?

Resources