Bil1
Bil1

Reputation: 438

Why is this ajax request failing?

This is probably a simple problem but I don't understand why this is happening. Here is the reduced code:

Ajax call :

mydata = {'action':'update','options':options};
        console.log(mydata);
        $.ajax({
            url: 'dt/scripts/stoplight.php',
            data: mydata
            }).success(function(data){
            if (data == 1) {
                alert("Options Updated");
            }else{
                alert(data);
            }
        })

My data looks like this:

action
    "update"
options
    Object { OMS-S="0", OMS-N="0", OHS="0"}

To clarify, this is a copy and paste from the browser console. The object is a valid object and is being passed via get like so:

https://*pathtomysite*/dt/scripts/stoplight.php?action=update&options%5BOMS-S%5D=0&options%5BOMS-N%5D=0&options%5BOHS%5D=0

This request hangs indefinately.

https://*pathtomysite*/dt/scripts/stoplight.php?action=update&options%5BOMS-S%5D=0&options%5BOMS-N%5D=0&options%5BOHS%5D=1

To further clarify. Options is being generated like this:

$("#stoplight_apply").click(function(){
                var radios = $("#stoplight_options").find("input:radio:checked");
                options = {};
                $.each(radios, function( key, value) {
                    options[value.name] = value.value;
                });
                set_stoplight_options(options);
            })

This one works fine.

If any of these options are set to anything other than 0 then the php script it is going to works great! If all of them are 0 then it hangs and loads indefinitely.

I commented out all the PHP that could be causing problems so currently the script does this:

$action = $_GET['action']; //Get or update
print_r($_GET['action']);
print_r($_GET['options']);

Why is this happening?

UPDATE:

I think I found the problem. All I did was change the word 'options' to 'test' and the php to print_r($_GET['test']) and it works fine. WTH?

Upvotes: -1

Views: 117

Answers (2)

andrew
andrew

Reputation: 9583

Try

 options

  Object { "OMS-S":"0", "OMS-N":"0", "OHS":"0"}

Now I see why quotes are a good practice :P

Upvotes: 0

l2aelba
l2aelba

Reputation: 22217

Try

Use $_POST not $_GET

$action = $_POST['action'];
print_r($_POST['action']);
print_r($_POST['options']);

Ajax :

var mydata = {'action':'update','options':options};
$.ajax({
   type: 'POST',
   url: 'dt/scripts/stoplight.php',
   data: mydata,
   success: function(data){
       if(data.length) {
         alert('done')
       }            
   }
});
  • Please do one by one (PHP -> JS -> HTML)
  • Ajax : post some data and get data like console.log(data); and see what you got.
  • Then do statement : if(data.length) if(data.sucess==='YeahDone') (JSON) whatever.. (check by your PHP script if all success echo 'YeahDone'; for example..
  • So do something with HTML or alert or whatever

Upvotes: 2

Related Questions