我想从其他微服务中使用 Laravel 5.8 中的 jQuery Ajax 获取动态依赖下拉列表?

如何解决我想从其他微服务中使用 Laravel 5.8 中的 jQuery Ajax 获取动态依赖下拉列表?

这里的极客是我的问题,当我更改项目名称时,它应该显示该项目的一些类似 URL,但它没有显示。但是当我检查值并检查它正在运行的日志时,响应即将到来,但在视图文件中它没有显示任何内容。能否请您检查以下文件以回答问题。

test.blade.php

@extends('layouts.backend.mainlayout')
@section('title','Seo Keyword View')

@section('content')
<input type="hidden" id="a_u_a_b_t" value="{!! $a_user_api_bearer_token !!}">
<script type="text/javascript">
localStorage.setItem('a_u_a_b_t',$('#a_u_a_b_t').val());
</script>
<!-- hidden Auth email and id for calling in json index -->
<input type="hidden" value="{{ Auth::user()->id }}" name="admin_id" id="auth_id" />
<input type="hidden" value="{{ Auth::user()->email }}" name="admin_email" id="auth_email" />

<div class="row page-titles">
<div class="col-md-5 align-self-center">
    <h3 class="text-themecolor">Account Admin</h3>
</div>
<div class="col-md-7 align-self-center">
    <!-- <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="javascript:void(0)">System Setting</a></li>
        <li class="breadcrumb-item active">Task</li>
    </ol> -->
</div>
</div>

<div class="container-fluid">

<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-body">
                <div class="table-responsive m-t-40">

                    {{-- model  open --}}
                    <div class="row">
                        <div class="col-md-8">
                            <button type="button" name="create_record" id="create_record"
                                class="btn btn-primary ">Add New Keyword</button>
                        </div>
                    </div>

                    <table id="keywordTable" class="table">
                        <thead>

                        </thead>
                        <tbody>

                        </tbody>

                    </table>

                    <div id="formModal" class="modal fade" role="dialog">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <div class="modal-header bg-primary">
                                    <h5 class="modal-title text-white" id="exampleModalLabel">Add New Keyword</h5>
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                        <span aria-hidden="true">&times;</span>
                                    </button>
                                </div>
                                <div class="modal-body">
                                    <!-- Success Message after submit -->
                                    <span id="form_result" aria-hidden="true"></span>
                                    <!-- Error Message after form not submit -->
                                   
                                    <form method="post" id="sample_form" class="form-horizontal"
                                        enctype="multipart/form-data">
                                        @csrf

                                        <div class="form-group" id="name_form">
                                            <label class="control-label col-md-4">Project Name</label>
                                            <div class="col-md-8">
                                                <select name="project_name" id="project_name" class="w-100 p-2">
                                                    <option value="default" class="form-control text-dark">Select Project </option>
                                                    @foreach(App\Http\Controllers\Api\ProjectApiController::getvalueall() as $project)

                                                    <option name="{{$project['id']}}" value="{{$project['project_name']}}">{{$project['project_name']}}</option>
                                                    @endforeach
                                                </select>

                                            </div>
                                        </div>

                                        <div class="form-group" id="name_form">
                                            <label class="control-label col-md-4">URL</label>
                                            <div class="col-md-8">
                                                <select name="url" id="url" class="w-100 p-2">
                                                <option value="" class="form-controll text-dark">Select URL </option>
                                                </select>

                                            </div>
                                        </div>

                                        <div class="form-group" id="name_form">
                                            <label class="control-label col-md-4">Keyword</label>
                                            <div class="col-md-8">
                                                <input type="text" name="keyword" id="keyword" class="form-control" />
                                                <!-- <span id="username" class="text-danger"></span> -->
                                            </div>
                                        </div>

                                        
                                        <!-- Sending admin_id and admin_email in hidden input box -->
                                        <input type="hidden" value="{{ Auth::user()->id }}" name="admin_id"
                                            id="admin_id" />
                                        <input type="hidden" value="{{ Auth::user()->email }}" id="admin_email" name="admin_email" />
                                        <input type="hidden" value="" id="url_id" name="url_id" />
                                        <input type="hidden" value="" id="project_id" name="project_id">
                                </div>

                                <!-- <br /> -->
                                <div class="form-group text-center">
                                    <input type="hidden" name="action" id="action" />
                                    <input type="hidden" name="hidden_id" id="hidden_id" />
                                    <input type="submit" name="action_button" id="action_button"
                                        class="btn btn-warning float-center" value="Add" />
                                </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <div id="confirmModal" class="modal fade" role="dialog">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header bg-light">
                                <button type="button" class="close" data-dismiss="modal">&times;</button>
                                <br>
                                <span class="modal-title_delete">Confirmation</span>
                            </div>
                            <div class="modal-body">
                                <h4 class="text-center" style="margin:0; color:red;">Are you sure you want to remove this URL?</h4>
                            </div>
                            <div class="modal-footer">
                                <button type="button" name="ok_button" id="ok_button"
                                    class="btn btn-danger">OK</button>
                                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                            </div>
                        </div>
                    </div>
                </div>

                {{-- model close --}}

            </div>
        </div>
    </div>
</div>
</div>
</div>
@endsection
@push('js')

<script>
$(document).ready(function() {
// onchange function for url get via project name
$('#project_name').on('change',function(){
    console.log('project name change');
    if(this.value !='default'){
        ajaxCall();
    } else{
        $('#url').empty();
        $('#url').append('<option> Select URL</option>');
    }
    function ajaxCall() {
    console.log('in ajaxCall');
    var project_id = $("#project_name option:selected").attr("name");
    console.log(project_id);
    $.ajax({
            type: "GET",url: '{{url('api/v1/j/addurl/getProject')}}/'+project_id,data: {},headers: {
                "Authorization": "Bearer " + localStorage.getItem('a_u_a_b_t')
            },success: function(response) {
            $('#url').empty();
            $('#url').append('<option> Select Url </option>');
            console.log(response);
            console.log(response.data);
            console.log(response.success);
            if(response.success){

                $.each(response.data,function(i,link) { 
                    $('#url').append('<option name="'+link.id+'" value="' + link.url + '">' + link.url + '</option>');
                });
            }
            },error: function(errorResponse) {
            console.log(errorResponse);
            }
        });
    }
});
// onchange function for url get via project name  
</script>
@endpush

TestApiController.php(我得到链接的控制器)

        <?php

    namespace App\Http\Controllers\Api;

    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;
    use GuzzleHttp\Client as Client;
    use Illuminate\Support\Facades\Log;
    use Config;
    use Validator;
    use Auth;

    class LinkApiController extends Controller
    {
        //generating access token as static for getvalueall function
        private static function getAddUrlAccessToken()
        {
            Log::info('In LinkApiController->getAddUrlAccessToken()');
            try{
                $http = new Client(); //GuzzleHttp\Client
                $response = $http->post(
                    Config::get('app.SD_ADD_URLS_MS_BASE_URL') . Config::get('app.SD_ADD_URLS_MS_OAUTH_TOKEN_URL'),[
                        'form_params' => [
                            'grant_type' => Config::get('app.SD_ADD_URLS_MS_GRAND_TYPE'),'client_id' => Config::get('app.SD_ADD_URLS_MS_CLIENT_ID'),'client_secret' => Config::get('app.SD_ADD_URLS_MS_SECRET'),'redirect_uri' => '',],]
                );
                $array = $response->getBody()->getContents();
                $json = json_decode($array,true);
                $collection = collect($json);
                $access_token = $collection->get('access_token');
                Log::info('Got the token!');
                return $access_token;
            } catch(RequestException $e){
                Log::info('There is some exception in LinkApiController->getAddUrlAccessToken()');
                return $e->getResponse()->getStatusCode() . ': ' . $e->getMessage();
            }
        }

        //get project function
        public function getUrl($id)
        {
            try{
                Log::info('SD_ADD_URLS_MS_ALL_URL: ' . Config::get('app.SD_ADD_URLS_MS_ALL_URL'));
                $access_token = $this->getAddUrlAccessToken();
                $url = ''
                .Config::get('app.SD_ADD_URLS_MS_BASE_URL')
                .Config::get('app.SD_ADD_URLS_MS_ALL_URL')
                .'/'
                .$id;
                Log::info('Got the access token from LinkApiController::getAddUrlAccessToken(). Now fetching URLs!');
                Log::info('ALL Addurls URL: ' . $url);
                $guzzleClient = new Client(); //GuzzleHttp\Client
                $params = [
                    'headers' =>[
                        'Accept' => 'application/json','Authorization' => 'Bearer ' .$access_token
                    ]
                ];
                $response = $guzzleClient->request('GET',$url,$params);
                Log::info('Got the Response from SD ADDurls MS');
                Log::info('Store hone ke baad index page par value aa raha hai !');
                $json = json_decode($response->getBody()->getContents(),true);
                Log::info('Number of objects in response: ' . count($json['data']));
                return $json;
            } catch(\Exception $e){
                Log::info('There was some exception in LinkApiController->getProject()');
                return $e->getResponse()->getStatusCode(). ':' . $e->getMessage();
            }
        }

        // onchange function via project_id
    public static function getProjectid($project_id)
        {
            Log::info('in LinkApicontroller->getProjectid(sadfasdfasdfasdfasdf)'.$project_id);
            try{
                $access_token = LinkApiController::getAddUrlAccessToken();
                Log::info('Got the access token from LinkApiController->getAddUrlAccessToken(). Now fetching urls!');
                $http = new Client(); //GuzzleHttp\Client
                $response = $http->get(
                    Config::get('app.SD_ADD_URLS_MS_BASE_URL') . Config::get('app.SD_ADD_URLS_MS_GETPROJECT_URL')
                    .'/'
                    .$project_id,[
                        'headers' => [
                            'Accept'     => 'application/json','Authorization' => 'Bearer ' . $access_token
                        ]
                    ]
                );
                Log::info('Got the response from URL!');
                $json = json_decode($response->getBody()->getContents(),true);
                Log::info('Number of objects in response: ' . count($json['data']));
                return $json['data'];
            } catch(\Exception $e){
                Log::info('ye aa gye ham error part me');
                return $e->getResponse()->getStatusCode() . ': ' . $e->getMessage();
            }
        }
    }

api.php(路由)

<?php

    use Illuminate\Http\Request;


    Route::group(['prefix'=>'v1/j','middleware' => 'auth','middleware' => 'client_credentials'],function(){
            
            // web url api route
            Route::get('addurl/index/{id}','Api\LinkApiController@getUrl')->name('addurl.index');
            Route::post('addurl/store/{project_name}','Api\LinkApiController@storeUrl')->name('addurl.store');
            Route::get('addurl/edit/{id}','Api\LinkApiController@editUrl')->name('addurl.edit');
            Route::post('addurl/update','Api\LinkApiController@updateUrl')->name('addurl.update');
            Route::get('addurl/destroy/{id}','Api\LinkApiController@destroyUrl')->name('addurl.delete');
            Route::get('addurl/getProject/{project_id}','Api\LinkApiController@getProjectid')->name('addurl.getProject');

    });

谷歌浏览器检查结果 enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res