duanjiao8007 2017-11-25 22:29 采纳率: 100%
浏览 88
已采纳

在ajax调用之后获取旧数据(get方法)Laravel 5.5

I'm using two ajax call, on one page: At one side of the page I use ajax post to post data and submit color in a database(table name: color_store) and on the other side I try to get all the colors from that table and put it in a select tag by clicking to a button. But I keep receiving the old data even if I submit a new color.

Here is my color submit form:

    <form method="post" action="/CRM/defineColor" id="colorPickingForm">

        <input id="colorName"  name="colorName" type="text"> 
        <button type="button" class="btn blue" id="addColor">submit</button>

    </form>

Here is my ajax to submit that color to the database:

$('#addColor').click(function(){
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            var  colorName = $('#colorName').val();

            $.ajax({
                url: "/CRM/defineColor",
                type:"POST",
                data:{ colorName: colorName},
                success: function( data ) {
                       $( "#result" ).empty().append( data )
                    }
              })

Here is the select tag and button that shows data:

   <select class="form-control" id="colorFetch">

   </select>
    <button type="button" class="btn red" id="colorRefresh">Color Update</button>

Here is the ajax call that gets all the colors:

    $.ajax({
                url: "/CRM/fetchColor",
                type:"get",
                dataType: "json",
                cache: false,
                contentType:"application/json; charset=utf-8",
                success: function( colorNameReq ) {


                    $("#colorRefresh").click(function(){

//loop through json object
                        $("#colorFetch").empty();
                        $.each( colorNameReq, function( key, value ) {


                            $('#colorFetch')
                                .append($("<option></option>")
                                    .attr({
                                        id:key,
                                        value:value
                                    })
                                    .text(value));

                        });

                    });
                }
            })

And here is my controller that gets all the colors from database:

     protected function fetchColor(){


        $colorInfo = new Color_store;
        $colorInfo = $colorInfo->get();
        $count = count($colorInfo);

        for($i = 0 ; $i < $count; $i++ ){
            $colorName[] = $colorInfo[$i]->colorName ;
        }

        $colorName=json_encode($colorName);

        return $colorName;
    }

So why am I getting the old values of JSON object, even after submitting the color into database? I don't really understand, isn't ajax suppose to give me the new data?

For example: When I load the page and press "Color update" button it shows me the two color that I submit before(red and green).But when I submit new color to the database, let's say yellow. after pressing the "Color update" button it shows me red and green again but no yellow on the list. isn't it suppose to show the new color in the list(red, green and yellow)?

Any help would be appreciated!

  • 写回答

1条回答 默认 最新

  • dr200166 2017-11-26 09:51
    关注

    Your color loading AJAX call seems not right. It should be as following:

      $("#colorRefresh").click(function(){
           $.ajax({
                    url: "/CRM/fetchColor",
                    type:"get",
                    dataType: "json",
                    cache: false,
                    contentType:"application/json; charset=utf-8",
                    success: function( colorNameReq ) {
    
                        //loop through json object
                            $("#colorFetch").empty();
                            $.each( colorNameReq, function( key, value ) {
    
    
                                $('#colorFetch')
                                    .append($("<option></option>")
                                        .attr({
                                            id:key,
                                            value:value
                                        })
                                        .text(value));
    
                            });
                  }
           });
      });
    

    And make sure whether the new color you added was successfully written to the database.

    Also, you could use the set interval function in order to check for the color updates in a given time interval. That would look like:

    setInterval(function(){ 
     $.ajax({
                        url: "/CRM/fetchColor",
                        type:"get",
                        dataType: "json",
                        cache: false,
                        contentType:"application/json; charset=utf-8",
                        success: function( colorNameReq ) {
    
                            //loop through json object
                                $("#colorFetch").empty();
                                $.each( colorNameReq, function( key, value ) {
    
    
                                    $('#colorFetch')
                                        .append($("<option></option>")
                                            .attr({
                                                id:key,
                                                value:value
                                            })
                                            .text(value));
    
                                });
                      }
               });
    }, 2000); //This would check and update in every 2 seconds. 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?