Dips
Dips

Reputation: 3290

jQuery AJAX sends data but fires error

I have a weird problem while processing a form from AJAX.

It is working as expected but for some reason it fires an error instead of success.

This is the code:

$(".sendBtn").click(function(e) {

    campaigncode = "#####";
    senderemail = "[email protected]";
    subject = "Test";
    sendermessage = "Test";
    targetURL = "www.abc.com";

    email = $(".email").val();

    //Email Validation
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;  
    if( email ==""){//Empty Check
        alert('Please enter your email');
        e.preventDefault();
    } else {
        if(!emailReg.test( email )) {//Email validation
            alert('*Please enter valid email');
            e.preventDefault();
        } else {

    //Ajax Start
    $.ajax({
            url: "http://services.ninemsn.com.au/sendtofriend/sendtofriendService.aspx?showdefaultmessage=true",
            context: document.body,
            type: "POST",
            data: {campaigncode:campaigncode, recipientemail:email, senderemail:senderemail, subject:subject, sendermessage:sendermessage, targetURL:targetURL},
            dataType: "jsonp",
            success: function() {
                alert('Success');
            },
                        error: function() {
                                    alert('Error');
                              }

        });//Ajax End

        }
    }

});

Upvotes: 2

Views: 925

Answers (3)

davefrassoni
davefrassoni

Reputation: 312

The error is in the server, send it with "response . AllowGet"

Upvotes: 0

Scotty
Scotty

Reputation: 2510

From your error, looks like this question and that question are similar. The "jQuery_172blah was not called" error is referring to a jsonp callback (which wasn't called because parsing somewhere else failed). I'd suggest...

  1. Try setting crossDomain: true, then also set dataType:text or dataType:text json.
  2. Try passing data as a string instead of a dict.
  3. Try setting jsonp:false and jsonpCallback. See the documentation on these.

For example:

var jsonString = JSON.stringify({
    recipientemail: email
});
$.ajax({
    url: "http://services.ninemsn.com.au/sendtofriend/sendtofriendService.aspx?",
    crossDomain: true,
    dataType: 'text json',
    data: jsonString,
    ...

Upvotes: 2

Tola
Tola

Reputation: 2421

JSONP requires that the response be wrapped in some kind of callback function.

Try to log the response to the console to see what was sent back

success: function(data) { console.log(data); }

Also, try wrap the response into an object using $.parseJSON:

success: function(data) {
    var json = $.parseJSON(data);
}

Upvotes: 0

Related Questions