在编译.php第8518行中的MethodNotAllowedHttpException

时间:2016-01-26 17:49:45

标签: laravel-5 salesforce apex

我将来自salesforce的请求发送到laravel然后laravel返回结果并将其显示在visual force页面上

错误明细

enter image description here

Apex方法

 public List<SelectOption> getItems()
    {
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();

        req.setEndpoint('http://clozer.3spire.net/public/goclozer/country');
        req.setMethod('GET');

        req.setCompressed(false);
        req.setBody('key1=value1&key2=value2');
        req.setHeader('Content-Type', 'application/json');  

         try {
            res = http.send(req);                   
        } catch(System.CalloutException e) {
            system.debug('Callout error: '+ e);         
        }                

        getAllCountry = (Map<String, String>)JSON.deserialize(res.getBody(),Map<String, String>.class);

        List<SelectOption> option = new List<SelectOption>();

        option.add(new SelectOption('0','--None--'));        
        for(String c : getAllCountry.values())
        {
            option.add(new SelectOption(c,c));
        }

        return option;
    }

预期结果

{"0":"Aruba","1":"Antigua and Barbuda","2":"United Arab Emirates","3":"Afghanistan","4":"Algeria","5":"Azerbaijan","6":"Albania","7":"Armenia","8":"Andorra","9":"Angola","10":"American Samoa","11":"Argentina","12":"Australia","13":"Ashmore and Cartier Islands"}

Laravel 5 Route

Route::get('/goclozer/country','GoClozerController@getCountry');

Laravel 5方法

public function getCountry()
    {
        $country = \App\Country::all();
        $names = array();

        foreach($country as $c)
        {
            $names[] = $c->name;
        }
        echo json_encode($names,JSON_FORCE_OBJECT);
    }

我怎样才能解决这个错误 提前致谢

1 个答案:

答案 0 :(得分:0)

    

  • MethodNotAllowedHttpException意味着您使用了错误的HTTP动词(Get,Post,Put,Delete ...)。您为GET定义的路线,但您可能正在发布数据
  •     
  • 修改(我假设您只想检索国家/地区名称)可以通过

    来实现
    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/([^/]+)/?$ /$1 [L,R=301]
    

    这只会从表格中检索国家/地区的名称,您可以根据需要添加更多字段。     

  •     
  • Controller获取请求,返回响应。您没有回复任何回复。只是回应结果。您可以执行以下操作,

        $countries = Country::all(['name']);
    

    这将只返回带有国家/地区名称的JSON。     

  •     
  • 您不必在路线声明的前面加上明确的斜线。你甚至可以像下面那样写,也可以。

        return $countries;