我正在从闪电组件中调用示例Google图书URL,因为我已经编写了APEX控制器来发出http请求。但它为Google Book API引发了404错误。这是我的APEX控制器,请检查,
public class WebservicesController {
public static String responseFormat='application/json';
public static String bodyContentType='application/json';
@AuraEnabled
public static Response makeRequest(String url, String method, String bodyContent) {
System.debug('Making request httpResponse ' );
HttpRequest request = buildRequest(url, method,bodyContent);
HttpResponse httpRes = sendRequest(request);
Response restRes = buildResponse(httpRes);
return restRes;
}
private static HttpRequest buildRequest(String url, String method, String bodyContent) {
HttpRequest request = new HttpRequest();
System.debug('Making request httpResponse '+ url );
request.setEndpoint(url);
request.setMethod(method);
request.setHeader('Content-Security-Policy', '*');
if (bodyContent != null) {
request.setBody(bodyContent);
request.setHeader('Content-Type', bodyContentType);
}
request.setHeader('ACCEPT', responseFormat);
return request;
}
private static HttpResponse sendRequest(HttpRequest request) {
return new Http().send(request);
}
private static Response buildResponse(HttpResponse httpRes) {
Response restRes = new Response();
restRes.status = httpRes.getStatus();
restRes.statusCode = httpRes.getStatusCode();
restRes.body = httpRes.getBody();
System.debug(' Status code is ' + restRes.statusCode );
System.debug(' httpResponse ' + httpRes.getBody() );
return restRes;
}
public class Response {
@AuraEnabled
public String status { get; set; }
@AuraEnabled
public Integer statusCode { get; set; }
@AuraEnabled
public String body { get; set; }
}
}
以及我正在调用此顶点控制器的助手js控制器方法如下..
createCustomer: function(component, customer) {
var action = component.get("c.makeRequest");
action.setParams({
url: "https://www.googleapis.com/books/v1/volumes/NFPqCQAAQBAJ",
method: "GET",
bodyContent: "",
});
action.setCallback(this, function(a) {
action.setCallback(this, function(response){
var state = response.getState();
if (state === "SUCCESS") {
var customers = component.get("v.data");
customers.push(response.getReturnValue());
component.set("v.data", customers);
}
var action = component.get("c.saveCustomer");
action.setParams({
"customer": customer
});
});
$A.enqueueAction(action);
});
$A.enqueueAction(action);
},
我还在远程设置中提供了这个google api url,并在我的域中添加为CSP可信站点。
答案 0 :(得分:0)
在以下方法前面添加@AuraEnabled: