-1

Users are able to enter a URL into a text field, I would like to add a jQuery based feature that would be able to check if a page at least exists so that I could warn a user if they may have accidentally entered an incorrect URL, before they hit "save"

Currently I am trying to use the following code, though it seems to always return the "Error: Does not exist" message:

$('#Inventory_edocsUrl').blur(function(){
    $.ajax({
        url:$('#Inventory_edocsUrl').val(),
        type:'GET',
        success: function()
        {
            console.log('Success, file exists!');
        },
        error: function()
        {
            console.log('ERROR:Does not exist');
        }
    });
});

I tested with http://google.com as a test url in the text field and it still returned the error. Upon switching the url to google.com (not http), I got the same error message along with another error message:

GET http://localhost/inventory/web/inventory/google.com 404 (Not Found) 

It makes sense that this second version does not work as there is no http:// and therefore the error should be returned, but I don't see why the first correct url is returning the programmed error message, but is actually returning no console errors like the second trial.

Is there something I am doing wrong?

ComputerLocus
  • 3,448
  • 10
  • 47
  • 96

2 Answers2

-1
$.ajax({
    statusCode: {
        404: function() {
            alert("page not found");
        }
    }
});

Form jQuery.ajax() documentation....

Virus721
  • 8,061
  • 12
  • 67
  • 123
-2

Use:

url:$('#Inventory_edocsUrl').text();

Instead of:

url:$('#Inventory_edocsUrl').val();
Josh
  • 2,835
  • 1
  • 21
  • 33
Rawlins
  • 61
  • 7