dousong2023 2013-09-03 12:37
浏览 36
已采纳

为什么这个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 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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料