95 可读性
 将光标移到/点击文章中的句子上,可以查看译文。      显示繁体中文内容    显示简体中文内容

SALESFORCE1(AURA PLATFORM): How to pass values from a client-side-code(JavaScript code) to server-side-code(APEX)?
SALESFORCE1 ( AURA平台): 如何将值从客户端代码( JavaScript代码) 传递到服务器端 code(APEX)?

I am working on Salesforce1 (Aura platform).i am trying to pass values from client-side (javascript) to server-side controller(Apex-code).i tried by using setParams(); in JavaScript and @key annotation in Apex but @key is not supported in Apex.

Thanks in advance.

I am giving sample code here...

APPLICATION code :

 <aura:application>
 <PlumQ:example/>
 </aura:application>

COMPONENT code :

<aura:component model="PlumQ.ExampleServerSideController">
 <aura:attribute name="firstName" type="String" default="HELLO worlD"/>
 <ui:inputtext label="Name" aura:id="id1" value="{!v.firstName}" placeholder ="enter name"/>
 <ui:button label="Native Aura Button" press="{!c.echo}"/>
</aura:component>

**client-side-controller(JAVASCRIPT):**

 ({
"echo" : function(component) {
 alert('in client-Side-process');
 var b =component.get("v.firstName");
 alert('firstnaaaaame:::::::::::::'+b);
 var a = component.get("m.serverEcho");
 alert('After ServerSide Process');
 a.setParams({ firstName : component.get("v.firstName") });
 a.setCallback(this, function(action) {
 if (action.getState() ==="ERROR") {
 alert("Server Error:" + action.getError()[0].message);
 }
 else{
 alert("From server:" + action.getReturnValue());
 }
});
 $A.enqueueAction(a);

} })

server-side-controller(APEX CLASS) :

 public class ExampleServerSideController {
 @AuraEnabled
 public static String serverEcho(@Key("firstName") String firstName){
 System.out.println("In Example Trival controllerrrrr"+firstName);
 return ("From server:" +firstName);
 }
 }
时间:原作者:3个回答

You don't need the @key notation for that, a String should work.

Try to put the $A.enqueueAction(a); inside you closure like this :

({
"echo" : function(component) {
 alert('in client-Side-process');
 var b =component.get("v.firstName");
 alert('firstnaaaaame:::::::::::::'+b);
 var a = component.get("m.serverEcho");
 alert('After ServerSide Process');
 a.setParams({ firstName : component.get("v.firstName") });
 a.setCallback(this, function(action) {
 if (action.getState() ==="ERROR") {
 alert("Server Error:" + action.getError()[0].message);
 }
 else{
 alert("From server:" + action.getReturnValue());
 }
 $A.enqueueAction(a);
 }
 });
原作者:

Your code is almost right.you only need to put quotation marks( " ) around firstName and remove @Key annotation from your apex controller.so your JS would look like :

a.setParams({
"firstName" : component.get("v.firstName")
});

And your apex :

public static String serverEcho(String firstName)
原作者:
...