2

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 = "test@email.com";
    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

        }
    }

});
Colin Brock
  • 21,267
  • 9
  • 46
  • 61
Dips
  • 3,220
  • 3
  • 20
  • 21
  • 3
    Is the AJAX event firing from the same domain or cross domain? – Praveen Kumar Purushothaman Jul 20 '12 at 02:29
  • fires from cross domain but it works cause I can receive email but just alert error – Dips Jul 20 '12 at 02:34
  • Guessing his site isn't ninemsn, so it's cross-domain. @PraveenKumar However it's JSONP, which is designed for cross-domain. – Scotty Jul 20 '12 at 02:34
  • yes your are right Scotty. jsonp works for cross domain – Dips Jul 20 '12 at 02:35
  • Change to `error: function(jqXHR, textStatus, errorThrown)` so you can get more information and paste it here. – Scotty Jul 20 '12 at 02:36
  • It just alert error. But how do you get value of them – Dips Jul 20 '12 at 02:42
  • @Dips: `error: function(jqXHR, textStatus, errorThrown) { alert('Error: ' + textStatus + ' ' + errorThrown); }` – Scotty Jul 20 '12 at 02:45
  • try Scotty's suggestion and alert textStatus – Leon Jul 20 '12 at 02:45
  • here is the error `Error: parsererror Error: jQuery172010452208149023878_1342752436990 was not called` – Dips Jul 20 '12 at 02:47
  • What do you mean by "It is working as expected"? Other than calling your error function what happens? Does NineMSN provide an API reference for the send to friend service? – nnnnnn Jul 20 '12 at 02:53
  • It functions what I wanted to do. It sends the data to server but just responds error instead success – Dips Jul 20 '12 at 02:56
  • It just seems a bit weird to me that the only data you're supplying to the service is an email address - how does it know what to send to that address? Again, is there an API reference for that service? – nnnnnn Jul 20 '12 at 02:57
  • It is send mail service. yes it has API ref. – Dips Jul 20 '12 at 03:00
  • 1
    OK, I guess I assumed it was obvious I was asking you to supply a link to the API reference. At the time I thought maybe I could have a bit of a look at the reference to see if your code was missing anything. Now I'm more or less out of patience... – nnnnnn Jul 20 '12 at 03:16
  • I have updated with all the code that I have – Dips Jul 20 '12 at 03:23

3 Answers3

2

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,
    ...
Community
  • 1
  • 1
Scotty
  • 2,480
  • 2
  • 16
  • 20
  • But using `dataType: 'text json',` doesn't send the data – Dips Jul 20 '12 at 03:04
  • @Dips: Try passing `data` as a JSON string, see suggestion above. – Scotty Jul 20 '12 at 03:22
  • No progress. Still same problem – Dips Jul 20 '12 at 03:37
  • @Dips: Which version of jQuery are you using? The parsererror sounds like a bug, or some argument not configured correctly... – Scotty Jul 20 '12 at 04:06
  • @Dips: If you can figure out what's causing the parsererror, that will be it. I've added one more suggestion above. I really think your `data` needs to be a **string** and use `dataType:json`. I'm guessing ajax() is causing a parser error because data is a dict... – Scotty Jul 20 '12 at 06:36
0

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);
}
Tola
  • 2,401
  • 10
  • 36
  • 60
0

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

davefrassoni
  • 306
  • 2
  • 5