why when I run this code on local server, req.send(sData) doesn't work?
It runs alert1 but doesn't run alert2;
function initialize() {
var req;
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (oc) {
req = null;
}
}
if (!req && typeof XMLHttpRequest != "undefined") {
req = new XMLHttpRequest();
}
return req;
}
function getData(sUrl, sData) {
var req = initialize();
req.open("POST", sUrl, false);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
alert("1");
req.send(sData);
alert("2");
return req.responseText;
}
sUrl and sData are the same as the original website sends.
Related
I need to send a ajax request to my server before web page close, my send code is below.
SendByAajx = function(msg) {
var response;
var xmlHttpReg;
if(window.XMLHttpRequest){
xmlHttpReg = new XMLHttpRequest();
} else if(window.ActiveXObject) {
xmlHttpReg = new ActiveXObject("Microsoft.XMLHTTP");
} else {
throw new Error("Unsupported borwser");
}
if(xmlHttpReg != null) {
xmlHttpReg.open("get", "https://127.0.0.1:57688/test"+'?'+msg, false);
xmlHttpReg.send(null);
if(xmlHttpReg.readyState==4){
if(xmlHttpReg.status == 200) {
var data = JSON.parse(xmlHttpReg.responseText);
if(typeof(data.errorcode) == "number" &&
data.errorcode != 0) {
throw("response error:" + data.errorcode);
}
response = data.result;
} else {
throw new Error("Error");
}
}
}
return response;
}
When I call this function in a button onclick event, it works.
function GetOnClick() {
try{
var result = SendByAajx (“data”);
} catch (e) {
//alert(errorInfo);
}
SetButtonDisabled(false);
}
But when I call this function when the page is unloaded, it doesn't work.
<body onload="javascript:OnLoad();" onunload="javascript:OnUnLoad()">
function OnUnLoad() {
try{
var result = SendByAajx(“data”);
} catch (e) {
//alert(errorInfo);
}
}
When I debug the application, the JS execution stops after this line:
xmlHttpReg.send(null);
It didn’t go to the next line:
if(xmlHttpReg.readyState==4)
The “data” is also not sent to the server.
What is wrong with my program, can ajax be called in an onunload function? What should I do to make it work?
I am building a windows phone mobile app with phonegap. I need to get some content from a particular page within the application...This is what I have for now:
function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
try {
req = new ActiveXObject('Msxml2.XMLHTTP');
} catch (err2) {
try {
req = new ActiveXObject('Microsoft.XMLHTTP');
} catch (err3) {
req = false;
}
}
}
return req;
}
var first_req = getXMLHTTPRequest();
var firsturl = '/books/Col.xml';
first_req.open('GET', firsturl, true);
first_req.onreadystatechange = createResponse;
first_req.send(null);
function createResponse()
{
if (first_req.readyState == 4) {
if(first_req.status == 200)
{
navigator.notification.alert(
'sucess',
doNot,
'success',
'Worked'
);
}
else {
navigator.notification.alert(
"request.status = "+first_req.status,
doNot,
'Error',
'Try Again'
);
}
it doesn't work, the else block runs and alerts request.status = 0
Any help would be really appreciated
I found a reference for an XMLHttpRequest
final req = new XMLHttpRequest();
req.open('GET', '${Sections.home}/data/$name', false);
req.send();
_htmlBody = req.responseText;
Full Source
But this is for async = false, is there a working example of async = true?
There is an example at https://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/samples/belay/bcap/bcap_client.dart
void privateInvoke(String ser, String method, String data,
SuccessI ski, FailureI fki) {
if (urlRegex.hasMatch(ser)) {
var req = new XMLHttpRequest();
req.open(method, ser, true);
req.on.readyStateChange.add(void _(evt) {
if (req.readyState == 4) {
if (req.status == 200) {
ski(req.responseText);
} else {
fki(new BcapError(req.status, req.statusText));
}
}
});
req.send(data);
return;
} else {
super.privateInvoke(ser, method, data, ski, fki);
}
}
also another asynchronous example in https://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/client/samples/total/src/ServerChart.dart
XMLHttpRequest request = new XMLHttpRequest();
request.on.readyStateChange.add((Event event) {
if (request.readyState == XMLHttpRequest.DONE && request.status == 200) {
callback("data:image/png;base64,${StringUtils.base64Encode(request.responseText)}");
}
});
...
request.open("POST", url, true, null, null);
request.setRequestHeader("Content-type", "text/plain");
request.overrideMimeType("text/plain; charset=x-user-defined");
print("Chart request: ${data}");
request.send(data);
Is it possible adapt this code for crossdomain and how
function makeRequest(url) {
var http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create an XMLHTTP instance');
return false;
}
http_request.onreadystatechange = function() { alertContents(http_request); };
http_request.open('GET', url, true);
http_request.send(null);
}
function alertContents(http_request) {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
receiveData(http_request.responseText);
} else {
alert("Îòâåò ñåðâåðà ïîëó÷åí, íî åñòü îøèáêà");
}
}
}
The same origin policy prevents JavaScript reading data from different origins under normal circumstances.
You can work around with:
A proxy for the data on the page's origin
JSONP
CORS (limited browser support, but possibly good enough for prime time now)
I am creating an XMLHTTP object in Firefox and IE8 and i transfer a very large amount of data (json). In Firefox it renders it in 5 seconds, in IE8 it takes 30 seconds.
Is there any way to make IE8 XMLHTTP works faster?
Here is my code for creating the XHR object:
function createRequestObject2()
{
var http_request = false;
if(window.XMLHttpRequest)
{ //: Mozilla, Safari,...
http_request = new XMLHttpRequest();
if(http_request.overrideMimeType)
{
//: set type accordingly to anticipated content type
//http_request.overrideMimeType('text/xml');
}
}
else if(window.ActiveXObject)
{ //: IE
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP.3.0");
}
catch (e)
{
try
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) { }
}
}
if(!http_request)
{
alert('Cannot create XMLHTTP instance');
return false;
}
return http_request;
}
Here is how i use the XHR:
http = createRequestObject2();
http.open('GET', encodeURI(action), true);
http.onreadystatechange = results;
http.send(null);
inwork = true;
viewLoading();
//: search results handling
function results()
{
/*console.log("do_search.results[ " +
'Results return ' + http.readyState
+ " ]"
);*/
if(http.readyState != 4)
return;
inwork = false;
if(http.status != 200)
{
//setStatusLine(httpapplicant.statusText);
return;
}
//: evaluate
var data = eval('(' + http.responseText + ')');
//: add data to the table
applicant_list = new dogtable(data, table1_options, $$('dog_results'));
hideLoading();
} //function results