Link to home
Start Free TrialLog in
Avatar of SmashAndGrab
SmashAndGrab

asked on

Jquery question

Hi,

I am hoping someone can help me with an issue I am having whilst using Jquery and Classic ASP.

This code works perfectly..


  <script type="text/javascript">

    $(document).ready(function() {
             $('ul.sf-menu').sooperfish();
    });

</script>


BUT as soon as I add in the code for my ajax form submit it does not work..


  <script type="text/javascript">
    $(document).ready(function() {
       
        
         $('#submitButton').click(function() {
    $.ajax({
            type: "POST",
            url: "test.asp",
            data:  $("#form1").serialize(),
            cache: false,
            dataType: "html",
            success: function(responseText){
                alert(responseText);
            },
            error: function(resposeText){
                alert(resposeText);
            },
        });

    return false;
});

        
             $('ul.sf-menu').sooperfish();
 
    });
Avatar of Big Monty
Big Monty
Flag of United States of America image

what "doesn't work"? do you get an error? does the form not submit? does your computer smoke? :P

maybe check your javascript console and see if there's an error there.
Have you checked your console for errors?
Unless there is a direct relationship between what the AJAX does and what sooperfish does then there should be no problem with this code.

Do you have a link we can look at?

Some notes on your code
You can replace
$(document).ready(function()

Open in new window

with
$(function()

Open in new window

You can do your serialize on the form like this
$(this.form).serialize()

Open in new window

All form elements keep a reference to the form they belong to - saves you having to give the form an id

return false works but better to capture the event parameter and use preventDefault - that way you don't have to worry about different execution paths potentially bypassing the return false
$('#submitButton').click(function(e) {
   e.preventDefault();

Open in new window


success has been deprecated in favour of the promise functions .done() (jQuery 1.5+) and then() (jQuery 1.8+)
Avatar of SmashAndGrab
SmashAndGrab

ASKER

Thanks for the comments.

Sorry - I should have been clearer.

1.  As soon as I add the ajax code - the sooperfish does not take effect (the menu at the top of the page).
2.  The Ajax form does not submit.

I will check the console.


@Julien - I don't really understand what you mean, are you suggesting I change the code to this:

 $(function(){
       
	   
	   $('#submitButton').click(function() {
    $.ajax({
            type: "POST",
            url: "AddPageUpdate.asp",
            data:  $("#newAdForm").serialize(),
            cache: false,
            dataType: "html",
            success: function(responseText){
                alert(responseText);
            },
            error: function(resposeText){
                alert(resposeText);
            },
        });

    return false;
});

Open in new window


Where should the other items that you mentioned go?

Thanks
console results:

User generated image
User generated image
it looks as if there is some issue with the ajax query code
ok I found the issue.

It was a comma in the incorrect place.



Here is corrected code:  

 $(function() {
       


        
             $('ul.sf-menu').sooperfish();
 
 
           $('#submitButton').click(function(e) {
               e.preventDefault();

          $.ajax({
            type: "POST",
            url: "AddPageUpdate.asp",
            data: $(this.form).serialize(),
            cache: false,
            dataType: "html",
            success: function(responseText){
                alert(" It was successful: "+responseText);
            },
            error: function(responseText){
                alert(" It failed: "+responseText);
            }
        });

    return false;
});
      
 
    });


However, the ajax call fails...
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To check the AJAX
1. Open the site in Firefox or Chrome
2. Open the Console
3. Load the page
4. Look for the Request appearing in the console - in FireFox it will be in the console tab with the Net option selected
     Chrome in the Network tab
5. Open the URL Response tab and look for errors there.

What the error is saying is not the AJAX call failed - it worked it just went to a resource that does not exist.
Sorry - I am talking bollocks! :)

Here is my latest code:

<script type="text/javascript">
    
	$(function() {
	// ADD EVENT PARAMETER TO EVENT HANDELER AND ...
	$('#submitButton').click(function(e) {
		// INVOKE event.preventDefault()
		e.preventDefault();
		
		$.ajax({
            type: "POST",
            url: "pages/add/AddPageUpdate.asp",
			// USE form PROPERTY ON BUTTON TO GET ACCESS TO FORM
            data:  $(this.form).serialize(),
            cache: false,
            dataType: "html"
		})
		// INSTEAD OF success AND error
		.done(function(responseText) {
                alert(responseText);
		})
		.fail(function() {
                alert("Error");
        });

		return false;
	});
	
	$('ul.sf-menu').sooperfish();
});
  </script>

Open in new window



Here's the console results:
User generated image
What version of jQuery are you using?

if < 1.5 you should consider updating it.
I found a serverside error:

User generated image
That does not solve the $.ajax( ).done is not a function though - you need to check your jQuery version. If you are bound to the version you are using then you will need to go back to using success - but I would ask why you are bound to a version < 1.5
the error you are getting is due to the fact that you're trying to read binary data after establishing the object collection. in other words, you cannot use Request.Form/QueryString AFTER you initialize the object to read the binary data.

I'm assuming you're using aspUpload for image file uploads, so you're code would look something like:
objUpload = Server.CreateObject("Persits.Upload.1")
someFormValue = objUpload.Form("someValue")
.....

Open in new window


if you use a Request.Form statement AFTER the first line above, you'll get the error. Change those references to objUpload.Form("xxx") and the error should be resolved.
@Big Monty - I am going to raise a new question for this.  - I will send you a link once it is open :)