I know this question has been asked before, but I tried to apply the answers with no results.
I'm trying to do multiple requests on the same domain with a for loop but it's working for the entire record of my array.
Here is the code I use:
function showDesc(str) {
var prod = document.getElementsByName("prod_item[]");
var xhr = [], i;
for (i = 0; i < prod.length; i++) {
var txtHint = 'txtHint10' + i;
(function(i) {
var xhr = new XMLHttpRequest();
var url = "getDesc.php?q=" + str;
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
document.getElementById(txtHint).innerHTML = xhr.responseText;
}
};
xhr.open("GET", url, false);
xhr.send();
})(i);
}
}
PHP
<select name="prod_item[]" id="prod_item.1" onchange="showDesc(this.options[this.selectedIndex].value)"></select>
<div id="txtHint100"></div>
and then I will use dynamic table for the prod_item field and div_id.
Is there any mistake in my code?
I'm trying to make a form in a website.
This form works sending with Ajax some data in the url to a php file and showing the response.
Is there any way to add a captcha in the url of the ajax and make the php check the captcha?
Thanks
My idea of Script:
<script>
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds) {
break;
}
}
}
function loadDoc() {
var cap = document.getElementById('captcha_code').value
document.getElementById("captcha_code").value = "";
var search = document.getElementById('nombre').value
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("box").innerHTML = xhttp.responseText;
sleep(500);
eval(document.getElementById("runscript").innerHTML);
}
};
xhttp.open("GET", "request.php?nombre=" + escape(search) + "&code=" + cap, true);
xhttp.send();
}
</script>
What crap is this?
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds) {
break;
}
}
}
You have something called setTimeout();. Please use that. And if you are using jQuery, please try this:
$.post(url, data, function (resp) {
// stuff to do when something returns from the server `resp`.
});
To get the captcha code in you PHP file you need to use $_GET('code') instead of GET(reCAPTCHA). You could also check if the permissions the php file has.
The idea is to start multiple xmlhttp requests in an loop and let them store their result in a global variable.
for (var i = 0; i < something; i++) {
fetch_site(url, i);
}
Now the function fetch_site() looks like this:
// the result will be palced at the i-th field in the "content"-array
function fetch_site(url, i) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", url, true);
// Milliseconds a request can take before automatically being terminated
xmlhttp.timeout = 500;
xmlhttp.ontimeout = function () {
content[i] = "none";
console.log("ontimeout: content[" + i + "] is now set to " + content[i] );
};
// On error
xmlhttp.onerror = function () {
content[i] = "none";
console.log("onerror: content[" + i + "] is now set to " + content[i] );
};
// On success
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
content[i] = xmlhttp.responseText;
// Deleting unneccessary spaces
content[i] = content[i].trim();
} else content[i] = "none";
}
};
//xmlhttp.setRequestHeader("Content-type","Lookup/simple");
xmlhttp.send();
}
I can confirm that each an every request gets send. However google-chrome marks them as pending and when diggin deeper it defines them as stalled.
By the way, this code is used in a chrome extension. Hence it might be a permission problem. Though I would not expect it to be one as I have granted my extension to fetch every site.
The corresponding manifest-file includes the following lines:
"permissions": [
"storage",
"http://*/",
"https://*/"
],
I've been trying to retrieve information about the latest commit of a specific repo using Javascript, but without many results.
I've been using a XMLHttpRequest to get the data from the repo's commits page (for example: https://api.github.com/repos/trapped/rotmg_svr/commits), but seems like it returns null.
Also, I tried to use regular expressions to get the first match of specific line values (for example, to get the value of the commit description, I used this one: "message": "(.*)", ), making it loop through all lines until it finds something (in the code I've posted I temporarily changed it to line numbers, trying to speed up the process since it wasn't working).
Also, when trying to load the page, it blocks. As my previous experience with C#'s webclient suggests me, it's probably related to the XMLHttpRequest thing (indeed when I tried to debug it was returning an undefined object).
Thanks in advance for all the help.
var messagelineindex = 14;
var linklineindex = 17;
var response = "";
function loadcommits() {
httpGet("https://api.github.com/repos/trapped/rotmg_svr/commits");
var lines = response.split("\n");
var message = parsemessage(lines[messagelineindex]);
var link = parselink(lines[linklineindex]);
addcommit(//+message+"");
//for (var i = 0; i < numlines; i++) {
// var line = lines[i];
// var commitmessage = parsemessage(line);
// if (commitmessage != "") {
// addcommit(commitmessage);
// break;
// }
//}
}
function parselink(text) {
var regex = /"link": "(.*)",/;
var match = regex.exec(text);
if (!match) {
return "";
}
else {
return match[1];
}
}
function parsemessage(text) {
var regex = /"message": "(.*)",/;
var match = regex.exec(text);
if (!match) {
return "";
}
else {
return match[1];
}
}
function addcommit(text) {
document.getElementById("commits").innerHTML = document.getElementById("commits").innerHTML + text.toString() + "\n\r";
}
function httpGet(theUrl) {
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
response = xmlHttp.responseText;
}
}
xmlHttp.open("GET", theUrl, false);
xmlHttp.send(null);
}
I want to execute FetchXML queries in a CRM 2011 environment using the CRM 2011 SOAP web services and JavaScript.
I have found a number of articles like this one showing how to use the 4.0 web service that is still available in the 2011 environment, but I do not want to do this.
This link seems to indicate that IOrganizationService.RetrieveMultiple can handle FetchXML. However, I do not want to use managed code for this.
I've come across this link that shows essentially what I want to do in the RetrieveMultiple function, but I want to be able to pass in existing FetchXML that I've written, not a new filter expression.
To execute fetchxml queries with JavaScript a bunch of frameworks /libraries are available:
XrmSvcToolkit
XrmServiceToolkit
CrmFetchKit.js
Instead of writing the code by hand these libraries provide a simple way to perform several operations and access the results. But take into account that not all libraries (currently) support cross-browser (Q2.2012).
From the link you posted to the Microsoft SDK, you will see how to connect to the ODATA service. As you may have already found, ODATA doesn't allow you to execute fetch.
Instead, you will need to use the SOAP service (/XrmServices/2011/Organization.svc), and pass your fetch using Retrieve Multiple.
Here's a more detailed look at using the 2011 service through JavaScript: http://blog.customereffective.com/blog/2011/05/execute-fetch-from-javascript-in-crm-2011.html
Here is another blog post which parses the returned XML and builds an easily consumable JavaScript object: http://blog.customereffective.com/blog/2011/05/parsing-and-consuming-the-crm-2011-soap-service-inside-javascript.html
The 2011 Organization Service is quite different in it's return, so it won't be plug-n-play from your 4.0 stuff; however, the 2011 endpoint has a lot of nice improvements.
The "Capture Sample HTTP Request and Response" section of this MSDN article outlines how to get a SOAP message that is sent to CRM 2011 from managed code.
The "Execute the Query" section of this MSDN article gives an example of using 2011's IOrganizationService.RetrieveMultiple in managed code to execute a FetchXML query.
Using both of these samples, you can extract a sample SOAP message for RetrieveMultiple that contains a FetchXML query.
The "Create a JScript Library" section of the first MSDN article shows how to perform an Execute request in JavaScript against the 2011 SOAP endpoint. Replace the Assign SOAP request in this example with the RetrieveMultiple SOAP message that you get from executing the managed code.
This enables you to execute a FetchXML request in JavaScript against the 2011 SOAP endpoint.
Here are some snippets from a JavaScript library I wrote using the information above:
(function (window, undefined) {
var _window = undefined;
if (window.Xrm)
_window = window;
else if (window.parent.Xrm)
_window = window.parent;
else throw new Error("Unable to determine proper window");
(function (Crm) {
(function (Service, $, JSON, Xrm) {
if (!Xrm)
throw new Error("Unable to locate Xrm");
if (!JSON)
throw new Error("Unable to locate JSON");
if (!$)
throw new Error("Unable to locate jQuery");
Service.Create = function (ODataSetName, EntityObject) {
if (!EntityObject) {
throw new Error("EntityObject is a required parameter");
return;
}
if (!ODataSetName) {
throw new Error("ODataSetName is a required parameter");
return;
}
var jsonEntityObject = JSON.stringify(EntityObject);
var req = new XMLHttpRequest();
req.open("POST", Service.GetODataEndPoint() + "/" + ODataSetName, false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
debuggingCallBack(this);
};
req.send(jsonEntityObject);
};
function debuggingCallBack(req) {
if (req.readyState == 4 /* complete */) {
if (req.status == 201 || req.status == 204 || req.status == 1223) {
//Success
//201 = create
//204 = update
//1223 = delete
}
else {
//Failure
debugger;
}
}
};
Service.Fetch = function (FetchXML) {
var request = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
request += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
request += "<s:Body>";
request += "<RetrieveMultiple xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
request += "<query i:type=\"a:FetchExpression\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">";
request += "<a:Query>";
request += Service.FetchEncode(FetchXML);
request += "</a:Query>";
request += "</query>";
request += "</RetrieveMultiple>";
request += "</s:Body>";
request += "</s:Envelope>";
var req = new XMLHttpRequest();
req.open("POST", Service.GetSOAPEndPoint(), false)
req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/RetrieveMultiple");
req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
req.setRequestHeader("Content-Length", request.length);
req.send(request);
results = Service.GetResults(req.responseXML);
return results;
};
Service.Delete = function (ODataSetName, EntityID) {
if (!EntityID) {
throw new Error("EntityID is a required parameter");
return;
}
if (!ODataSetName) {
throw new Error("ODataSetName is a required parameter");
return;
}
var req = new XMLHttpRequest();
req.open("POST", Service.GetODataEndPoint() + "/" + ODataSetName + "(guid'" + EntityID + "')", false)
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("X-HTTP-Method", "DELETE");
req.onreadystatechange = function () {
debuggingCallBack(this);
};
req.send();
};
Service.GetServerUrl = function () {
var serverUrl = null;
serverUrl = Xrm.Page.context.getServerUrl();
if (serverUrl.match(/\/$/)) {
serverUrl = serverUrl.substring(0, serverUrl.length - 1);
}
return serverUrl;
};
Service.GetODataEndPoint = function () {
return Service.GetServerUrl() + "/XRMServices/2011/OrganizationData.svc";
};
Service.GetSOAPEndPoint = function () {
return Service.GetServerUrl() + "/XRMServices/2011/Organization.svc/web";
};
Service.GetResults = function (responseXML) {
var sFetchResult = responseXML.selectSingleNode("//RetrieveMultipleResult").xml;
var oResultDoc = new ActiveXObject("Microsoft.XMLDOM");
oResultDoc.async = false;
oResultDoc.loadXML(sFetchResult);
var oResults = new Array(oResultDoc.firstChild.firstChild.childNodes.length);
var iLen = oResultDoc.firstChild.firstChild.childNodes.length;
for (var i = 0; i < iLen; i++) {
var oResultNode = oResultDoc.firstChild.firstChild.childNodes[i];
var oBE = new BusinessEntity(oResultNode.selectSingleNode("//a:LogicalName").text);
var iLenInner = oResultNode.firstChild.childNodes.length;
for (var j = 0; j < iLenInner; j++) {
var oRA = new Object();
var value = null;
if (oResultNode.firstChild.childNodes[j].lastChild.childNodes.length == 3) {
if (oResultNode.firstChild.childNodes[j].lastChild.getElementsByTagName("a:Id").length == 1)
value = oResultNode.firstChild.childNodes[j].lastChild.getElementsByTagName("a:Id")[0].text;
if (oResultNode.firstChild.childNodes[j].lastChild.getElementsByTagName("a:Value").length == 1)
value = oResultNode.firstChild.childNodes[j].lastChild.getElementsByTagName("a:Value")[0].text;
}
if (!value)
value = oResultNode.firstChild.childNodes[j].lastChild.text;
oRA["value"] = value;
oBE.attributes[oResultNode.firstChild.childNodes[j].firstChild.firstChild.text] = oRA;
}
oResults[i] = oBE;
}
return oResults;
};
Service.BusinessEntity = function BusinessEntity(sName) {
this.name = sName;
this.attributes = new Object();
};
Service.FetchEncode = function (FetchXML) {
var c;
var HtmlEncode = '';
if (FetchXML == null) {
return null;
}
if (FetchXML == '') {
return '';
}
for (var cnt = 0; cnt < FetchXML.length; cnt++) {
c = FetchXML.charCodeAt(cnt);
if (((c > 96) && (c < 123)) ||
((c > 64) && (c < 91)) ||
(c == 32) ||
((c > 47) && (c < 58)) ||
(c == 46) ||
(c == 44) ||
(c == 45) ||
(c == 95)) {
HtmlEncode = HtmlEncode + String.fromCharCode(c);
}
else {
HtmlEncode = HtmlEncode + '&#' + c + ';';
}
}
return HtmlEncode;
};
} (Crm.Service = Crm.Service || {}, _window.jQuery, _window.JSON, _window.Xrm));
} (_window.Crm = _window.Crm || {}));
} (window));
"REST is an architectural style in which every resource is addressed by using a unique URI."
http://msdn.microsoft.com/en-us/library/gg334279.aspx
You will not be able to use the REST end-point if you need to use FetchXml.
The alternative is to construct a SOAP message as you saw in your CRM4 examples. I haven't done this myself yet, but perhaps you could use a tool like Fiddler to see what the SOAP message look like so you can replicate them in your environment which your modified FetchXml.
Anyway, just an idea for now. Let me know how it goes, and if you manage to resolve it perhaps post your solution.