ajax parse xml response function wont recognize XML file URL - javascript

im using the following code from another solution on here
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type: "GET",
dataType: "xml",
url: "xmlfile.xml",
success: function(xml){
$("#output").append($(xml).find("address"));
}
});
});
</script>`
and if i use the local xmlfile.xml file it works fine, but when i try to run the same function with the xml file hosted elsewhere, it stops working:
http://www.zillow.com/webservice/GetUpdatedPropertyDetails.htm?zws-id=X1-ZWz1ebcem1ra4r_10irx&zpid=48749425
I tried encoding the URL using escape() and encodeURIComponent()
neither of those worked.
what am i doing wrong? why does the function work with a local xml file but breaks when i use the other URL?

Related

Post an XML request to the URL using JavaScript

I have an API URL where I want to post the XML data. My API URL only accept XML. I am posting my XML to URL using ajax.
Here is my XML
<?xml version="1.0" encoding="UTF-8"?>
<data>
<lead>
<key>*****</key>
<id>*****</id>
<data6>Lead has been updated. merchant</data6>
</lead>
</data>
and my JavaScript code is:
<button type="button" onclick="loadXMLDoc()">Add Quote</button>
<script>
function loadXMLDoc() {
var data = "<data><lead><key>*****</key><id><?php echo $id; ?></id><data6>Lead has been updated. merchant</data6></lead></data>";
$.ajax({ type: "POST",
url: "https://inspire.flg360.co.uk/api/APILeadCreateUpdate.php",
data: data,
contentType: "text/xml",
dataType: "xml",
cache: false,
error: function() { alert("No data found."); },
success: function(xml) {
alert("it works");
alert($(xml).find("project")[0].attr("id"));
}
});
}
</script>
When I click the add quote button then it goes into error block of the ajax function. I have given the data posting URL and XML data in the code.
I assume you are using this snippet inside a wordpress template.
The first thing is, you should enclose all your codes with jQuery No Conflict
The reason why it does not do anything is $ is not being recognized. So, replace $ by jQuery and then your code will work properly.
Make sure you have declared the $id variable inside a PHP block right before the script.
I have ran it here
Hope it works!

load external xml file into js variable to be using jquery

I am trying to load an xml file containing config for a single page apps logic using jquery.
I can get the console to show the xml has been loaded and even display the xml in console but have yet to be able to get the xml to be declared as a string variable. I am using the following code
$.ajax({
type: "GET",
url: "dummy.xml",
dataType: "xml",
success: function (url) {
console.log('started function xml');
console.log(url);
// Parse the xml file and get data
parseXMLConfig;
}
});
I have a separate function to parse the xml using jquery which works if I declare the xml directly in the JavaScript but need to separate the xml and js for later development efforts.
Can anyone tell me how to set the js object to a variable for use elsewhere in scripts.
You can do something like this
$.ajax({
type: "GET",
url: "dummy.xml",
success: function (xmContent) {
console.log(xmContent);
xmlDoc = $.parseXML(xmContent),
$xml = $( xmlDoc ),
$title = $xml.find( "title" );
console.log($title );
}
});
For more detail refer DOC

How to read local xml file using $.ajax?

I am trying to build a Firefox extension for which I need to read local xml file but does not able to read file using $.ajax. My code is like below:
$.ajax({
type: "GET",
url: "file:///C:/Users/Mitul Gandhi/Desktop/Library_en.xml",
dataType: "xml",
success: function (xml) { }
});
Due to security reasons javascript ajax calls cannot read local file contents.
To read a local file in a Firefox addon you can use Components (file IO) like so:
function Read(file)
{
var ioService=Components.classes["#mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var scriptableStream=Components
.classes["#mozilla.org/scriptableinputstream;1"]
.getService(Components.interfaces.nsIScriptableInputStream);
var channel=ioService.newChannel(file,null,null);
var input=channel.open();
scriptableStream.init(input);
var str=scriptableStream.read(input.available());
scriptableStream.close();
input.close();
return str;
}
var contents = Read("C:/Users/Mitul Gandhi/Desktop/Library_en.xml");

GeoServer get GeoJSON from AJAX issue

I am using GeoServer version 2.2.5, and what I try to do is making a AJAX call to get the json string from the output GeoJSON url, for example:
http://localhost:8080/geoserver/sf/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=sf:archsites&maxFeatures=50&outputFormat=json
the javascript I used is like this:
var processJSON = function (data) {
console.log(data);
};
function init() {
//geojson url
var url = "http://localhost:8080/geoserver/sf/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=sf:archsites&maxFeatures=50&outputFormat=json&&format_options=callback:processJSON";
//execuate ajax request to get data
$.ajax({
url: url,
dataType: 'jsonp',
jsonp: 'processJSON'
});
}
I am pretty sure that this method works, because I can get the json object from the url which is from GeoServer version 2.2.4. But it just doesn't work for GeoServer 2.2.5 and later. I read somewhere that says "JSONP support has been disabled by default since it is perceived as a security issue." But I have no idea how to make it work.
Can anyone give me some suggestion on this?
Thank you very much

javascript object to php

i'm trying to send an js object to a php function using jquery.ajax.
This is what i have so far:
js side:
$.ajax({
type: "GET",
dataType: "json",
url: url,
data: {persoon : persoon2},
async: false,
success: function(){
alert(data);
return true;
}
});
php side:
$decode = json_decode($_GET["persoon"]);
$verzekering->setVoornaam($decode->persoon_voornaam);
in js this works: persoon2.persoon_voornaam
but i can't get to the value in php, what am i doing wrong?
few fixes
data: "persoon=persoon2", // check input format
success: function(data) { // missing data argument
EDIT your ajax code is working (check URL or php code)
http://jsfiddle.net/ish1301/KZndE/
Found the problem(s)
I was using this inside Drupal and the Jquery version was still 1.2.6. Upgrading it resolved a lot of the problems
The string i tried to catch with the $_GET["persoon"] was mall formated becaus i just send along a js object. Changing
data: {persoon : persoon2},
to
data: {persoon:JSON.stringify(persoon2)},
fixed the problem

Categories

Resources