2

Here is my javascript:

function sendAttack(attacker,defender,hpChange){
    USER_LAST_TIMESTAMP = Math.round(new Date().getTime() / 1000.0);
    var attack = new Attack(TURN_ID,attacker,defender,hpChange);
    attack.action_sequence = ACTION_QUEUE;
    var attackJSON = JSON.stringify(attack);
    jQuery.ajax({
        type: "POST",
        url: urlLeader + "attacks.json",
        data: attackJSON,
        success: function(data){
            ACTION_QUEUE++;
        }
    })  
}

In setup.js loaded prior to the above:

jQuery.ajaxSetup({
    accepts: 'application/json',
    dataType: 'jsonp'
});

This is what the server receives when I call that function:

2012-04-26T03:57:44+00:00 app[web.1]: Started GET "/attacks.json?callback=jQuery17103089843031743851_1335412646794&{%22attacker%22:2,%22defender%22:3,%22hp_change%22:6.799999999999997,%22action_sequence%22:0}&_=1335412659141" for 167.206.19.130 at 2012-04-26 03:57:44 +0000

Not the "GET". What's going on here?

6
  • First, have you tried to use different data as part of your post? Hard code a value, like data: "test=ing" or data: { test: 'ing'}. Second, are you using json2.js to enable "JSON.stringify" for browsers that don't natively support it? (see stackoverflow.com/questions/1480393/…) Commented Apr 26, 2012 at 4:15
  • You are omitting the dataType parameter in your request; jQuery may be changing your request type for you. Commented Apr 26, 2012 at 4:15
  • dataType is for the return value. It would be more likely if a "contentType" was specified to impact sending data to the server. Are you changing the jQuery AJAX defaults anywhere? Commented Apr 26, 2012 at 4:17
  • It seems you are sending JSON to the server in which case you need to set processData to false and set the appropriate Content-Type else JQuery tries to convert the data into application/x-www-form-urlencoded MIME type. Commented Apr 26, 2012 at 4:28
  • Default type is "GET". Try to complete jQuery.ajax function with semi-colon (;) and then check it once. There may be a problem with syntax on browsers. Commented Apr 26, 2012 at 4:44

2 Answers 2

2

maybe this forum post at jquery.com describes your problem:

Cross-domain JSONP requests are created by using a dynamic script tag, so they can only use a GET method.

Sign up to request clarification or add additional context in comments.

Comments

0

I suspect this block of jQuery code is responsible:

// Handle cache's special case and global
jQuery.ajaxPrefilter( "script", function( s ) {
    if ( s.cache === undefined ) {
        s.cache = false;
    }
    if ( s.crossDomain ) {
        s.type = "GET";
        s.global = false;
    }
});

i.e. a cross-domain script request (as used by JSONP) gets its type: field reset to GET.

This is because JSONP doesn't use XMLHTTPRequest at all, it fakes up a <script> tag and then your browser creates its own GET request to retrieve that resource.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.