salesforce响应中的跨源问题(Access-Control-Allow-Origin)

时间:2017-01-03 06:56:03

标签: javascript jquery salesforce salesforce-service-cloud salesforce-communities

我尝试使用JS从外部本地文件中获取salesforce的记录。 我可以在网络选项卡中看到响应。 我在控制台中收到错误消息:

  

" XMLHttpRequest无法加载https://login.salesforce.com/services/oauth2/token。 No' Access-Control-Allow-Origin'标头出现在请求的资源上。起源' null'因此不允许访问。"

mycode的:

  public class MainActivity extends AppCompatActivity {
    private static final int CAMERA_REQUEST = 1888;
    public static final String ALLOW_KEY = "ALLOWED";
    public static final String CAMERA_PREF = "camera_pref";
    Button Close;
    Button reload;
    ImageView imageView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        openCamera();


    }
    private void openCamera() {//i open the camera here
            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            startActivityForResult(intent, CAMERA_REQUEST);

        }

        @Override
        public void onActivityResult(int requestCode, int resultCode,  Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == CAMERA_REQUEST) {
                initiatepopupwindow()
                Bitmap photo = (Bitmap) data.getExtras().get("data");
                imageView.setImageBitmap(photo);
            }
        }
    private PopupWindow pwindo;

    private void initiatePopupWindow() { 

       LayoutInflater inflater = (LayoutInflater) MainActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View layout = inflater.inflate(R.layout.layout,
                (ViewGroup) findViewById(R.id.popup));
         imageView = (ImageView) this.findViewById(R.id.imageView1);
        android.os.Handler handler = new android.os.Handler();
        try {
            handler.postDelayed(new Runnable() {
                public void run() {
                    pwindo = new PopupWindow(layout, 700, 770, true);
                    pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
                }
            }, 100);


            Button close = (Button) layout.findViewById(R.id.button3);
            Button reload = (Button) layout.findViewById(R.id.button2);

            close.setOnClickListener(new OnClickListener() {

                public void onClick(View popupView) {
                    pwindo.dismiss();
                }
            });
            reload.setOnClickListener(new OnClickListener() {

                public void onClick(View popupView) {
                    openCamera();
                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

任何帮助和建议。

1 个答案:

答案 0 :(得分:0)

在您的选项中添加crossOrigin: true

$.post("https://login.salesforce.com/services/oauth2/token",
{
    grant_type:"password",
    dataType : 'jsonp',
    crossOrigin : true,   /// Add this option 
    headers : {Accept : "application/json","Access-Control-Allow-Origin" : "*"},
    client_id:"CLIENTID",
    client_secret:"CLIENTSECRET",
    username: "uname",
    password: "password"
},
function(data,status){
    //my_function(data);
    console.log(data);
});
function my_function(data){
    alert(data);
}