I have a Html file locally with the following code
<h1>This is session token </h1>
<div id="noob"></div>
<script src="http://code.jquery.com/jquery-1.9.1.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
var sessiontoken;
var randomUser = Math.round(Math.random() * 1000000);
var supportCors = $.support.cors;
var sessiontoken ;
$.support.cors = true;
$.ajax({
type: 'POST',
url: "https://abc.com/Gateway.Session/Session",
dataType: "json",
data: {
UserId: "TestUser" + randomUser,
CSK1: "abc",
CustId: "cde"
},
success: function (data) {
$.support.cors = supportCors;
sessiontoken=data.Token;
alert(sessiontoken);
document.getElementById('noob').innerHTML = sessiontoken;
},
error: function (xhr, textStatus, error) {
$.support.cors = supportCors;
alert("responseText: " + xhr.responseText);
alert("XHR statusText: " + xhr.statusText);
alert("textStatus: " + textStatus);
alert("error: " + error.message);
}
});
});
</script>
This code when run locally(ex: c:/development/mypage.html) is generating session token after enabling the pop-up 'allow running javascript' " of IE7.
i.e., ajax function in above javascript is returning 'success'
when run locally and session token is generated.
But when this code is deployed in server and opened in IE7 like (http:// localhost:8080/mypage.html), here ajax function of above script is returning 'error'.
As when we run the file locally we enabled the pop-up to allow running script.
But when the file is deployed in server we won't get such pop-up as javascript runs itself when a html page is accessed from server.
I am unable to figure out why control going to 'error' part o 'success' when we file from server
So how can i make this ajax function to return to 'success' part when accessed from server.
The file executes fine in chrome when accessed locally or from server.Now i need it to be working with IE7.
Related
I'm referring a question answered here.
Trying to append an external PHP file in to jQuery and tried load.
$(".chatbox").load("./one.php");
This gives me the output;
Your success message!
However the concern is this removes all HTML in the body and does not really 'append' the success message.
I tried the following instead.
$(".chatbox").append("./one.php");
which merely prints this!
./one.php
Am I missing something here?
The .load() load data from the server and place the returned HTML into the matched element. But you need to use $.ajax() or $.get() that get data and return it into callback function.
$.get("./one.php", function(data) {
$(".chatbox").append(data);
});
in case if the page you're trying to load is failing due to some reason, you also need to handle the error block to inform you about the issue. Here is a more comprehensive way to do it. Here I have called a wiki page but you will know, all the php pages are in fact interpreted as valid html by PHP engine :)
$.ajaxPrefilter( function (options) {
if (options.crossDomain && jQuery.support.cors) {
var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
}
});
$.ajax({
type: "GET",
url: "https://en.wikipedia.org/wiki/PHP",
data: { },
success: function(data){
$('#demo').html(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
$('#demo').html("Status: " + textStatus + "<br/>Error: " + errorThrown);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="demo"></div>
I have been looking at converting a web app to an Electron desktop app. I am starting an http server in main.js to handle ajax requests from my app. I've created a button and defined its onclick function to call an ajax function and then update another element when the response arrives, e.g. $('#message').text('whatever'). This all works but the element ('#message') is not completely refreshed - I see the new text on top of the old text. If I click somewhere in the window the element is refreshed properly. I've tried calling blur() on the element and setting the focus to another element, but none of that helps.
Why does the BrowserWindow not erase the element's old text when I update it via jQuery, and how can I fix it?
Thanks in advance for your help!
Additional details
Here is the code that is called when the "Test" button is clicked.
function test_webapi() {
var url = 'http://localhost:8888/time';
console.log("Sending request: " + url);
$.ajax({
cache: false,
method: 'GET',
url: url,
error: function(xhr, status, err) { console.log("Ajax error from " + url + ": " + err); },
success: function(data) {
console.log("Have response from " + url, data);
$('#message').text(data.Data.Time);
}
})
}
And the code that sets up the http server and handles the ajax request.
var port = 8888;
var server = http.createServer(handle_time_request);
server.listen(port, function() {
console.log("API server listening on port " + port);
});
function handle_time_request(req, res) {
var tval = moment(new Date()).format('YYYY-MM-DD hh:mm:ss');
var reply = {
Success: true,
Message: "whatever",
Data: { Time: tval }
};
res.writeHead(200, {"Content-Type": "application/json"});
res.end(JSON.stringify(reply, null, 4));
}
I have problem with get the returned data from a link . It is not working .
I have search more and try everything but it still not working . could anyone help me .
the link : https://www.routingnumbers.info/api/data.json?rn=13442323
my code is
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
var request = $.ajax({
type: 'GET',
data: "rn=" + "54455445",
url: "https://www.routingnumbers.info/api/data.json",
success: function(data) {
alert("Data: " + data + "\nStatusA: " + status);
console.log(data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert("Data: " + "Err" + "\nStatus: " + status);
console.log(jqXHR, textStatus, errorThrown);
request.abort();
}
});
$.get("demo_test.asp", function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
</head>
<body>
<button>Send an HTTP GET request to a page and get the result back</button>
</body>
</html>
Thank you
Well, first of all, I'd always advise using proper JSON formatting in ajax requests. The data attribute should be an object instead of a variable assignment. In the end, it doesn't actually matter; the request will complete with either format. It's always nice to look at consistently-formatted code, though. Regardless, the major problems that were causing the request to fail were the type of request you were making and its origin.
In the code you provided, you were making a request to an https address. The page you were requesting from, however, was not an https page, but an http page instead. This caused the request to return the error net::ERR_INSECURE_RESPONSE.
If you fix that, however, the request still won't complete successfully. This is because XMLHttpRequest doesn't allow requests to websites that aren't the same as the one that you are requesting from. For example, you could request content from routingnumbers.info if you're running the script from within routingnumbers.info itself, but not if you're running it from elsewhere. This can be fixed by specifying the request as a jsonp request instead of the default json request.
$("button").click(function(){
$.ajax({
type: "GET",
dataType: "jsonp",
async: true,
url: "http://www.routingnumbers.info/api/data.json",
data: {
rn: "54455445"
},
success: function(data) {
var array = [];
$.each(data, function(index, value){
array.push(index + ': ' + value);
});
alert(array.join("\n"));
},
error: function(data) {
alert(data.statusText);
}
});
});
JSFiddle
Hope this is helpful!
I've have created a PHP web service method
public function import_external_xml($importXml)
I want to allow a client to upload xml via my web service method. My web service is not on the same domain as the client. The client has a webpage with a button where he want to write some javascript/jQuery to upload the xml via my web service method.
How can he do this?
Web service method in server.php:
public function import_external_xml($importXml)
{
echo 'import_external_xml';
exit;
}
I did the same thing using Ruby on Rails. But was sending data that was not in xml format.
One important thing as you said that your webservice is not on the same domain, you will have to deal with CORS. You can get idea about CORS here https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
You can refer this Post JQuery JSON Calls To PHP WebService Always Runs "Error" Callback
function callPhpAPI(){
var dataa = {"your_data":youdata};
$.ajax({
url : yourwebserviceurl,
type: 'POST',
data : JSON.stringify(dataa),
contentType : "application/json",
success:function(data)
{
if(data){
alert(data);
//console.log("Data from Server"+JSON.stringify(data));
}
else{
console.log("Data is empty");
}
},
error: function(xhr) {
alert('Error! Status = ' + xhr.status + " Message = " + xhr.statusText);
//console.log('Error! Status = ' + xhr.status + " Message = " + xhr.statusText);
}
});
});
You can call your php function like this using ajax
xmlDocument = $("<wrap/>").append(xmlDocument).html();
xmlDocument = encodeURIComponent(xmlDocument);
then ajax
jQuery.ajax({
type: "POST",
url: 'your_functions_address.php',
processData : false,
contentType : 'text/xml',
data : xmlDocument,
success: function (obj, textstatus) {
if( !('error' in obj) ) {
yourVariable = obj.result;
}
else {
console.log(obj.error);
}
}
});
i think you want pass xml via this ajax post , then you can refer this discussion
post xml
also check this
im implementing sign up with ajax on my site its working perfectly on desktop but causing problem in Android Browser The problem is after i click on signup button in android browser it post data to database but do not replace the html message.And alert native code error.
function postdata(){
var chkfrm = checkdata();
if(chkfrm == 0){
var url = '<?php echo base_url();?>index.php/Signup/signin';
$.ajax({
type: "POST",
url: url,
data: $("#formI").serialize(), // serializes the form's elements.
beforeSend:function(){
$("#signupdiv").html('<h1>Loadinng...........</h1>');
},
success:function(data)
{
$("#signupdiv").html(data);
},
error:function () {
alert(console.log);
}
});
e.preventDefault();
}
else {
$("#msgjava").html('<p>We need a little bit information from you.Please fill it.</p>');
return false;
}
You can't do e.preventDefault(); where you are because e is not passed into this function (thus it is undefined). This will cause an error and stop JS execution.
In what you posted, you are also missing a closing brace at the end of the postdata() function.
Your alert says "native code" because that's what this line of code:
alert(console.log)
will do. console.log is a native function so alerting a native function won't do anything useful. You need to make that alert a lot more useful. To see in more detail what error is coming back from the ajax function, change your error handler to something like this:
error: function(jqXHR, textStatus, errorThrown) {
alert("status = " + textStatus + ", errorThrown = " + errorThrown);
}
And, then see what it says.