Ajax cannot reach the post page - javascript

I've been spending hours trying to figure out what is wrong with this code, i found out that i cannot reach the updateuser.php file, its on the same directory, the filenames are corret, is there something wrong with this code:
<script>
function updateuseracc(form, password)
{
var p = document.createElement("input");
form.appendChild(p);
p.name="p";
p.type="hidden";
p.value=hex_sha512(password.value);
password.value="";
var useremail=$("#curemail").val();
$.post('updateuser.php',{email:useremail,p:p}).done(function(data){
alert(data);
});
}
</script>

Bring up development tools with F12 on Google Chrome, then go to 'Network' option and check if after calling the script a 500 header page is returned. You can check this at the list of resources called that you will see at development bar.
If so, you will know that you are not referring to the script correctly, also you will get information from the parsed URL so you can guess how can you set the path to updateuser.php.

Related

Is there a way to hide a link if the file does not exist on FTP using JavaScript/jQuery?

what I'm hoping to accomplish is if there isn't a file for a particular location then hide the link, if there is, show the link.
I have this piece of html here:
<p>[[C1:event_location]]
<br />[[C1:event_location_city]], [[C1:event_location_state]], [[C1:event_location_country]]
<br />Google Map
| Route Map
| 1km Route Map
| 5km Route Map
</p>
What's happening here is the Luminate platform using it's own custom tags you see there E130 and C1:event_title to check the href that is linking to the FTP folder for a pdf file name that starts with Route_map_ or Route_map_1k_ or Route_map_5k_ and ends with the name of the location. If the pdf file is there and you click the link it'll load the pdf file, however if there isn't a file there with that name it'll load a page not found.
Ex:
I'm on my website, and I'm looking at the location Abbotsford. In the ftp folder I have a file called ROUTE_MAP_1K_Abbotsford.pdf, but I do NOT have a file called ROUTE_MAP_5K_Abbotsford.pdf.
If I click the link with the 1k it'll load the pdf file, if I click the link with the 5k it'll load a page not found because the pdf file doesn't exist. So because "5km Route Map" file doesn't exist that element should be hidden.
What I'm hoping is possible, is that there is some JS/jQuery way of checking if the file exists if it doesn't then hide the link.
EDIT I realize after seeing some answers that I worded the question wrong. So what is happening is if I hover my mouse over the 5km Route Map link the browser shows (at the bottom) examplewebsite.com/documents/2018Maps/ROUTE_MAP_5K_Abbotsford.pdf even though the file isn't actually there, because the Luminate code is rendering that url so if you click the link it'll take you to examplewebsite.com/site/PageServer?pagename=page_not_found_Run (you can still navigate the site, doesn't give a real server 404 error). Is there a way to check if the file actually exists, if it doesn't then hide the link?
Thank you for your time!
I believe below is a bit slow as it will ajax every single one of your pdfs, but it will do the trick, since you seem to only be able to solve it from the client side and since you only really need fail/error with 404:
$("a[href^='pdf']").each(function() {
var $currentLink = $(this);
$.ajax($currentLink.attr("href")).fail(function(jqXHR, textStatus, errorThrown) {
if(jqXHR.status == 404 || errorThrown == 'Not Found') {
$currentLink.remove();
}
});
});
Here's a JS check for whether a file exists or not:
function UrlExists(url)
{
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
return http.status!=404;
}
This will check for a 404 status (file not found)
If you'd like to structure it to return when a file DOES exist, switch the status code from 404 to 200.
JQuery Solution:
$.get(url)
.done(function() {
// exists code
}).fail(function() {
// not exists code
})
For additional context, look into:
How do I check if file exists in jQuery or pure JavaScript?

Javascript Redirect sends visitors to Wrong URL

I have a redirect that happens after a user submits a form. Users should move through the funnel like this:
SiteA.com/submit-form/ ---> SiteB.com/payment/
Problem - The redirect ignores the domain name (i.e. SiteB). It sends visitors to SiteA.com/payment/
Here's the code:
jQuery(document).ready(function(){
var tracker = ga.getAll()[0];
var linkerParam = tracker.get('linkerParam');
var url = 'https://siteB.com/payment/?' + linkerParam;
window.setTimeout(window.location = url, 4000);
});
This code is meant to append Google Analytics cross-domain tracking code. The form plugin (Formidable Pro) also has a redirect option built-in. When activated, it also redirects to the wrong page.
I'm not sure where to start looking. I had added a Category Base in Permalinks. I've just removed it, hoping this might be the problem. Any ideas / help would be greatly appreciated!
window.location return object and in strict mode you will get exception in Chrome.
Full explanation you can find here
You just need
window.setTimeout(function(){window.location.href = url}, 4000);

Magento not saving product edits and redirects to dashboard?

When I try to edit an existing product on Magento, After hitting "Save and continue edit" button , It does not save anything at all and just goes to admin dashboard .
So I checked the "Network" tab in inspect element of Chrome browser and saw this error :
Here is the highlighted file (prototype.js) lines containing 1739 :
try {
var response = new Ajax.Response(this);
if (this.options.onCreate) this.options.onCreate(response);
Ajax.Responders.dispatch('onCreate', this, response);
this.transport.open(this.method.toUpperCase(), this.url,
this.options.asynchronous);
if (this.options.asynchronous) this.respondToReadyState.bind(this).defer(1);
this.transport.onreadystatechange = this.onStateChange.bind(this);
this.setRequestHeaders();
this.body = this.method == 'post' ? (this.options.postBody || params) : null;
/* line 1739 */ this.transport.send(this.body);
/* Force Firefox to handle ready state 4 for synchronous requests */
if (!this.options.asynchronous && this.transport.overrideMimeType)
this.onStateChange();
}
So how can I fix this problem ? What's wrong with this ?
I had the same error. The problem was in mod_secure. Changes in .htaccess don't help for me, I disabled it from cpanel for the domain and the problem was gone.
Please note, the issue appears only when the website works under https and when I use single HTML tags like in content (static block, cms pages, product attributes).
For more information please look here http://maxrice.com/magento-fixing-please-wait-error-adding-editing-products-categories/
The above image hints, that the issue is not directly code related forsay, but asset related. The fact that a simple css file is missing, and you have other network related errors before hand. It appears that prototype is not loading above which when referencing a function that library doesn't exist would cause a problem.. When did this start happening?

Changing a link dynamically in PhantomJS and clicking it to scrape the page

I've been trying to figure this out for a couple days now but haven't been able to achieve it.
There's this web page were I need to scrap all records available on it, I've noticed that if I modify the pagination link with firebug or the browser's inspector I can get all the records I need, for example, this is the original link:
<a href="javascript:gReport.navigate.paginate('paginator_min_row=16max_rows=15rows_fetched=15')">
If I modify that link like this
<a href="javascript:gReport.navigate.paginate('paginator_min_row=1max_rows=5000rows_fetched=5000')">
And then click on the pagination button on the browser (the very same that contains the link I've just changed) I'm able to get all records I need from that site (most of the time "rows" doesn't get any bigger than 4000, I use 5000 just in case)
Since I have to process that file by hand every single day I thought that maybe I could automatize the process with PhantomJS and get the whole page on a single run without looking for that link then changing it, so in order to modify the pagination link and getting all records I'm using the following code:
var page = require('webpage').create();
var fs = require('fs');
page.open('http://testingsite1.local', function () {
page.evaluate(function(){
$('a[href="javascript:gReport.navigate.paginate(\'paginator_min_row=16max_rows=15rows_fetched=15\')"]').first().attr('href', 'javascript:gReport.navigate.paginate(\'paginator_min_row=1max_rows=5000rows_fetched=5000\')').attr('id','clickit');
$('#clickit')[0].click();
});
page.render('test.png');
fs.write('test.html', page.content, 'w');
phantom.exit();
});
Notice that there are TWO pagination links on that website, because of that I'm using jquery's ".first()" to choose only the first one.
Also since the required link doesn't have any identificator I select it using its own link then change it to what I need, and lastly I add the "clickit" ID to it for later calling.
Now, this are my questions:
I'm, not exactly sure why it isn't working, if I run the code it fetches the first page only, after examining the requested page source code I do see the href link has been changed to what I want but it just doesn't get called, I have two different theories on what might be wrong
The modified href isn't getting "clicked" so the page isn't getting updated
The href does get clicked, but since the page takes a few seconds to load all results dynamically I only get to dump the first page Phantomjs gets to see
What do you guys think about it?
[UPDATE NOV 6 2015]
Ok, so the answers provided by #Artjomb and #pguardiario pointed me in a new direction:
I needed more debugging info on what was going on
I needed to call gReport.navigate.paginate function directly
Sadly I simply lack the the experience to properly use PhantomJS, several other samples indicated that I could achieve what I wanted with CasperJS, so I tried it, this is what I produced after a couple of hours
var utils = require('utils');
var fs = require('fs');
var url = 'http://testingsite1.local';
var casper = require('casper').create({
verbose: true,
logLevel: 'debug'
});
casper.on('error', function(msg, backtrace) {
this.echo("=========================");
this.echo("ERROR:");
this.echo(msg);
this.echo(backtrace);
this.echo("=========================");
});
casper.on("page.error", function(msg, backtrace) {
this.echo("=========================");
this.echo("PAGE.ERROR:");
this.echo(msg);
this.echo(backtrace);
this.echo("=========================");
});
casper.start(url, function() {
var url = this.evaluate(function() {
$('a[href="javascript:gReport.navigate.paginate(\'paginator_min_row=16max_rows=15rows_fetched=15\')"]').attr('href', 'javascript:gReport.navigate.paginate(\'paginator_min_row=1max_rows=5000rows_fetched=5000\')').attr('id', 'clicklink');
return gReport.navigate.paginate('paginator_min_row=1max_rows=5000rows_fetched=5000');
});
});
casper.then(function() {
this.waitForSelector('.nonexistant', function() {
// Nothing here
}, function() {
//page load failed after 5 seconds
this.capture('screen.png');
var html = this.getPageContent();
var f = fs.open('test.html', 'w');
f.write(html);
f.close();
}, 50000);
});
casper.run(function() {
this.exit();
});
Please be gentle as I know this code sucks, I'm no Javascript expert and in fact I know very little of it, I know I should have waited an element to appear but it simply didn't work on my tests as I was still getting the page without update from the AJAX request.
In the end I waited a long time (50 seconds) for the AJAX request to show on page and then dump the HTML
Oh! and calling the function directly did work great!
The href does get clicked, but since the page takes a few seconds to load all results dynamically I only get to dump the first page Phantomjs gets to see
It's easy to check whether it's that by wrapping the render, write and exit calls in setTimeout and trying different timeouts:
page.open('http://testingsite1.local', function () {
page.evaluate(function(){
$('a[href="javascript:gReport.navigate.paginate(\'paginator_min_row=16max_rows=15rows_fetched=15\')"]').first().attr('href', 'javascript:gReport.navigate.paginate(\'paginator_min_row=1max_rows=5000rows_fetched=5000\')').attr('id','clickit');
$('#clickit')[0].click();
});
setTimeout(function(){
page.render('test.png');
fs.write('test.html', page.content, 'w');
phantom.exit();
}, 5000);
});
If it's really just a timeout issue, then you should use the waitFor() function to wait for a specific condition like "all elements loaded" or "x elements of that type are loaded".
The modified href isn't getting "clicked" so the page isn't getting updated
This is a little trickier. You can listen to the onConsoleMessage, onError, onResourceError, onResourceTimeout events (Example) and see if there are errors on the page. Some of those errors are fixable by the stuff you can do in PhantomJS: Function.prototype.bind not available or HTTPS site/resources cannot be loaded.
There are other ways to click something that are more reliable such as this one.

Is it possible to trigger behavior in the content script from the popup?

Currently I am using message passing to send a request from my contentscript for data in localStorage and I am not having any issues with that, the content script is working as expected.
Can you do this in the other direction?
I have an object that exists in the content script that has a method called ".apply()" and I want to run it when the used clicks the option to do so.
I tried to make a listener in the content script like this:
var myLinker = new Linker();
chrome.extension.onRequest.addListener(function(request) {
if (request.method == "apply")
{
myLinker.apply("nothing");
alert("applied");
}
else
; //Do nothing
And send requests to it like this:
chrome.extension.sendRequest({method: "apply"}, function(){
alert("Tried to request");
});
I get that it is a bit of a hack, but it is the only thing I could think of, and it doesn't even work =/
Is there a way to do this?
I am pretty sure I could just inject new code into the page from the popup (I think I saw an api function for that), and then run stuff, but that would take more memory and just feels like a bad way to do it, because you would basically have the exact same code twice.
To send a message from the extension to a content script, use chrome.tabs.sendMessage instead of chrome.extension.sendRequest.
Because sendRequest has been superseded by onMessage in Chrome 20, there's no official documentation for sendRequest any more. The documentation for chrome.tabs.sendMessage can be found here. Please note that these events cannot be mixed, use either *Request or *Message.
Yes, you would use this: http://developer.chrome.com/extensions/tabs.html#method-sendMessage
Content scripts live within the DOM of the page. And each page that is open within Chrome has a tab ID associated with it -- http://developer.chrome.com/extensions/tabs.html#type-tabs.Tab
Let's say you want to send the {method: "apply"} to a page that was just opened in a new tab:
chrome.tabs.onCreated.addListener(function(tab) {
chrome.tabs.sendMessage(tab.id, { method: "apply" });
});
There are other events/methods to get the specific Tab you want to send the message to. I think there's one called getCurrent to send to the currently selected tab, check out the docs for that.

Categories