dousong2023
2013-09-03 12:37 阅读 30

为什么这个ajax请求失败了?

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    doqau82086 doqau82086 2013-09-03 12:41

    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
    点赞 评论 复制链接分享
  • douwen5681 douwen5681 2013-09-03 13:07

    Try

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

    Now I see why quotes are a good practice :P

    点赞 评论 复制链接分享

相关推荐