XMLHttpRequest. WSDL request. readyState == 4, but status == 0 - javascript

I try to parse WSDL information from http://rates.kazpost.kz/postratesprodv2/postratesws.wsdl
But when I try it, I have nothing.
xmlhttp.readyState == 4, but xmlhttp.status == 0.
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', 'http://rates.kazpost.kz/postratesprodv2/postratesws.wsdl', true);
// build SOAP request
var sr =
'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pos="http://webservices.kazpost.kz/postratesws">' +
'<soapenv:Header/>' +
'<soapenv:Body>' +
'<pos:GetPostRateRequest>' +
'<pos:GetPostRateInfo>' +
'<pos:SndrCtg>1</pos:SndrCtg>' +
'<pos:Product>P102</pos:Product>' +
'<pos:MailCat>1</pos:MailCat>' +
'<pos:SendMethod>2</pos:SendMethod>' +
'<pos:Weight>750</pos:Weight>' +
'<pos:Dimension>s</pos:Dimension>' +
'<pos:Value>100</pos:Value>' +
'<pos:From>050031</pos:From>' +
'<pos:To>010001</pos:To>' +
'<pos:ToCountry>au</pos:ToCountry>' +
'</pos:GetPostRateInfo>' +
'</pos:GetPostRateRequest>' +
'</soapenv:Body>' +
'</soapenv:Envelope>';
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
alert('done. use firebug/console to see network response');
} else {
alert('ERROR. Status: ' + xmlhttp.status + " " + xmlhttp.statusText);
}
}
}
// Send the POST request
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
xmlhttp.send(sr);
// send request
// alert("Ready.");

Seems like you're trying to make http request under https connection, so request is blocked by browser for security reasons.
HTTP Ajax Request via HTTPS Page for more details

Related

I've got "The object's state must be OPENED."

I've tried to perform AJAX request, but I've got this kind of error, that ajax call should be opened. But it is opened already. Trying to send the XMLHttpRequest header, but getting this kinda error. Guys, help me!
let del_btn = document.querySelectorAll('.delete');
for(let i = 0; i < del_btn.length; i++)
{
del_btn[i].addEventListener('click', function(){
let xhr = new XMLHttpRequest();
let book_title = del_btn[i].parentElement.children[1].children[1].children[0].children[0].textContent;
xhr.onreadystatechange = function()
{
if(xhr.readyState == 4 && xhr.status == 200)
{
if(this.responseText.includes('Deleted'))
{
let books = this.responseText.split('.')[1];
if(books == '0')
{
del_btn[i].parentElement.parentElement.innerHTML = 'You have no books in your list. Go to the ' + ' ' + ' main page ' + ' ' + 'and start your ride!';
}
else
{
del_btn[i].parentElement.parentElement.removeChild(del_btn[i].parentElement);
document.querySelector('.amount-b').textContent = 'Количество книг: ' + books;
}
}
}
}
xhr.open("POST", "../operations/delete_book.php", true);
xml.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send('title=' + book_title);
}, false);
}```

Cannot change created custom Status Reason via JavaScript

I have changed salesorder entity and create a new status reason in Submitted.
I have added a status reason named PreTransport in submitted state, with value 100000004.
I can go from Active state(pending status reason) to Submitted state(In-Progress status reason) via this request :
var request = ""
request += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
request += " <s:Body>";
request += " <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
request += " <request i:type=\"b:SetStateRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
request += " <a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
request += " <a:KeyValuePairOfstringanyType>";
request += " <c:key>EntityMoniker</c:key>";
request += " <c:value i:type=\"a:EntityReference\">";
request += " <a:Id>" +recordGuid+ "</a:Id>";
request += " <a:LogicalName>salesorder</a:LogicalName>";
request += " <a:Name i:nil=\"true\" />";
request += " </c:value>";
request += " </a:KeyValuePairOfstringanyType>";
request += " <a:KeyValuePairOfstringanyType>";
request += " <c:key>State</c:key>";
request += " <c:value i:type=\"a:OptionSetValue\">";
request += " <a:Value>"+stateCode+"</a:Value>";
request += " </c:value>";
request += " </a:KeyValuePairOfstringanyType>";
request += " <a:KeyValuePairOfstringanyType>";
request += " <c:key>Status</c:key>";
request += " <c:value i:type=\"a:OptionSetValue\">";
request += " <a:Value>"+statusCode+"</a:Value>";
request += " </c:value>";
request += " </a:KeyValuePairOfstringanyType>";
request += " </a:Parameters>";
request += " <a:RequestId i:nil=\"true\" />";
request += " <a:RequestName>SetState</a:RequestName>";
request += " </request>";
request += " </Execute>";
request += " </s:Body>";
request += "</s:Envelope>";
//send set state request
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web", true)
// Responses will return XML. It isn't possible to return JSON.
req.setRequestHeader("Accept", "application/xml, text/xml, */*");
req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
var successCallback = null;
var errorCallback = null;
req.send(request );
But it does not work from In-Progress to PreTransport that are in the same state.
I suppose that it because of permission. Should I set Permission on this new status reason?
If yes How can I do this?
Note that I have CRM 2011 on-premise.
Note: I have tried this code also, but no success.
XrmServiceToolkit.Soap.SetState("salesorder", Xrm.Page.data.entity.getId(), 1, 100000004,false);
Sorry I don't have access to 2011 version. But I tested this below snippet in CRM Rest builder, It works perfectly.
I have created a custom status reason (StatusCode) of value 100000000 : Shipping ready under status (StateCode) of 1 : Submitted. Make sure you are flowing through the allowed transitions like answered here.
var entity = {};
entity.StateCode = {
Value: 1
};
entity.StatusCode = {
Value: 100000000
};
XrmServiceToolkit.Rest.Update("B4B625A1-3789-E811-A967-000D3A1A9407", entity, "SalesOrderSet", function() {
//Success - No Return Data - Do Something
}, function(error) {
Xrm.Utility.alertDialog(error.message);
}, true);

Trying to display Json response in HTML using JS

I'm trying to display json on web page and i'm using the following code but it doesn't work, who can help me understand what am i doing wrong?
<button type="submit" onclick="javascript:send()">call</button>
<div id="div"></div>
<script type="text/javascript" language="javascript">
function send()
{
var xmlhttp = new XMLHttpRequest();
//xmlhttp.onreadystatechange = callbackFunction(xmlhttp);
xmlhttp.open("GET", "http://stam/1.1/json/url=https://www.google.co.il");
xmlhttp.setRequestHeader("Content-type", "application/json");
xmlhttp.send();
xmlhttp.responseType = 'json';
xmlhttp.setRequestHeader("Content-Type", "application/json");
//xmlhttp.onreadystatechange = callbackFunction(xmlhttp);
var requestDoneAndOK = false;
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState === 4) {
if (xmlhttp.status === 200) {
//requestDoneAndOK = true;
var jsonResponse = JSON.parse(xmlhttp.responseText);
document.getElementById("div").innerHTML = xmlhttp.statusText + ":" + xmlhttp.status + "<BR><textarea rows='100' cols='100'>" + jsonResponse + "</textarea>";
}
}
};
}
</script>
Your url is not correct. Also you should removed the commented out lines mentioned in the code to make it work.
You can use this dummy url to check if it's working:
https://reqres.in/api/users?page=2
function send()
{
var xmlhttp = new XMLHttpRequest();
//xmlhttp.onreadystatechange = callbackFunction(xmlhttp);
xmlhttp.open("GET", "http://stam/1.1/json/url=https://www.google.co.il");
xmlhttp.setRequestHeader("Content-type", "application/json");
xmlhttp.send();
// Remove the below commented lines and change the url
// xmlhttp.responseType = 'json';
// xmlhttp.setRequestHeader("Content-Type", "application/json");
// xmlhttp.onreadystatechange = callbackFunction(xmlhttp);
var requestDoneAndOK = false;
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState === 4) {
if (xmlhttp.status === 200) {
//requestDoneAndOK = true;
var jsonResponse = JSON.parse(xmlhttp.responseText);
document.getElementById("div").innerHTML = xmlhttp.statusText + ":" + xmlhttp.status + "<BR><textarea rows='100' cols='100'>" + jsonResponse + "</textarea>";
}
}
};
}
The URL you provided does not seem to work. Open the browser console (F12 and switch to the console tab). If you try http://date.jsontest.com/, for example, the browser console will log any errors.
For some reason, Firefox allowed me to make the request, but Chromium did not. Chromium's console provided the exact line numbers where there were issues.
I removed the second xmlhttp.setRequestHeader("Content-Type", "application/json");.
It also told me that the response type in xmlhttp.responseType = 'json'; should be text.
Here is the final product. Notice that I have to specify the key of the JSON object in order to get its value: jsonResponse.time
<button type="submit" onclick="javascript:send()">call</button>
<div id="div"></div>
<script type="text/javascript" language="javascript">
function send() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "http://date.jsontest.com/");
xmlhttp.setRequestHeader("Content-type", "application/json");
xmlhttp.send();
xmlhttp.responseType = 'text';
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState === 4) {
if (xmlhttp.status === 200) {
var jsonResponse = JSON.parse(xmlhttp.responseText);
document.getElementById("div").innerHTML = xmlhttp.statusText + ":" + xmlhttp.status + "<br><textarea rows='100' cols='100'>" + jsonResponse.time + "</textarea>";
}
}
};
}
</script>
To print out the entire JSON object:
// var jsonResponse = JSON.parse(xmlhttp.responseText); No need for this.
document.getElementById("div").innerHTML = xmlhttp.statusText + ":" + xmlhttp.status + "<br><textarea rows='100' cols='100'>" + xmlhttp.responseText + "</textarea>";
Also, the concept of unobtrusive JavaScript is worth taking a look at.

Post web service from javascript

I'm triying to post webservice from javascript. I'am using this test webservice to post. But If I look in firebug I get following exception:
XML parsing error: syntax error Location: moz-nullprincipal:{9e8dc1d9-98d5-48f5-9106-5a19cb9ca7aa} Column: 1, Row: 1:
Reload the page to get source for: http://www.w3schools.com/webservices/tempconv...
^
My code is shown below:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', 'http://www.w3schools.com/webservices/tempconvert.asmx', true);
// build SOAP request
var sr =
'<soapenv:Envelope' +
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
'xmlns:api="http://127.0.0.1/Integrics/Enswitch/API" ' +
'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">' +
'<soapenv:Body>' +
'<CelsiusToFahrenheit xmlns="http://tempuri.org/">' +
'<Celsius>44</Celsius>' +
'</CelsiusToFahrenheit>'+
'</soapenv:Body>' +
'</soapenv:Envelope>';
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
alert('done use firebug to see response');
}
}
}
// Send the POST request
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
xmlhttp.send(sr);
// send request
// ...
Have you any idea?
I found the problem.
I gave space near soapenv.
So it is look like now:
'<soapenv:Envelope' + ' ' +
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
'xmlns:api="http://127.0.0.1/Integrics/Enswitch/API" ' +
'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">' +
'<soapenv:Body>' +
'<CelsiusToFahrenheit xmlns="http://tempuri.org/">' +
'<Celsius>44</Celsius>' +
'</CelsiusToFahrenheit>'+
'</soapenv:Body>' +
'</soapenv:Envelope>';

Error with XSL validation of Javascript

I am getting validation errors such as: "No processing instruction starts with 'xml...." when I try to validate my code before I put it into the 3rd party app I'm using. How do I send a soap message with Javasript that is embedded on an XSL page without getting this error ? Here is the code in question:
<script language="javascript">
function test1(newSymbol){
var symbol = newSymbol;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "http://www.webservicex.net/stockquote.asmx?op=GetQuote",true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlhttp.responseText);
// http://www.terracoder.com convert XML to JSON
var json = XMLObjectifier.xmlToJSON(xmlhttp.responseXML);
var result = json.Body[0].GetQuoteResponse[0].GetQuoteResult[0].Text;
// Result text is escaped XML string, convert string to XML object then convert to JSON object
json = XMLObjectifier.xmlToJSON(XMLObjectifier.textToXML(result));
alert(symbol + ' Stock Quote: $' + json.Stock[0].Last[0].Text + '\n' + 'Company Name: ' + json.Stock[0].Name[0].Text);
document.getElementById('price').innerHTML = json.Stock[0].Last[0].Text;
document.getElementById('name').innerHTML = json.Stock[0].Name[0].Text;
}
else if (xmlhttp.readyState == 4 && xmlhttp.status != 200) {
alert('Server Issue');
}
}
xmlhttp.setRequestHeader("SOAPAction", "http://www.webserviceX.NET/GetQuote");
xmlhttp.setRequestHeader("Content-Type", "text/xml");
var xml = '<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
'<soap:Body> ' +
'<GetQuote xmlns="http://www.webserviceX.NET/"> ' +
'<symbol>' + symbol + '</symbol> ' +
'</GetQuote> ' +
'</soap:Body> ' +
'</soap:Envelope>';
xmlhttp.send(xml);
}
</script>
Im getting the error on the var xml = line right after I declare the soap headers.
Thanks for your help with this! :)
Just remove the XML declaration: it is not necessary in this case, because the version and encoding pseudo attributes specify the default values -- 1.0 and utf-8.

Categories