簡體   English   中英

Sendgrid與SAPUI5應用程序的集成在POST上返回“ 400 Bad Request”,但在Postman上有效

[英]Sendgrid integration with SAPUI5 app returns '400 Bad Request' on POST but works on Postman

我正在開發利用UI5庫的cordova混合應用程序。 我需要能夠從該應用程序作為電子郵件發送,因此我決定使用Sendgrid API,並且由於NodeJS模塊存在問題,因此我正在創建一個AJAX請求。 我以前已經用Postman測試了該請求,能夠成功發送電子郵件。 但是,當我嘗試從該應用程序執行相同的請求時,我偶然發現狀態為“ 400錯誤的請求”,而沒有進一步的信息。 代碼段如下:

            var settings = {
                "async": true,
                "crossDomain": true,
                "url": "https://api.sendgrid.com/v3/mail/send",
                "method": "POST",
                "headers": {
                    "Content-Type": "application/json",
                    "Authorization": "Bearer [REDACTED]",
                    "cache-control": "no-cache"
                },
                "processData": false,
                "data": {
                    "personalizations": [
                        {
                        "to": [
                            {
                                "email": "myemail@gmail.com"
                            }
                        ],
                        "subject": "Contato do app"
                        }
                    ],
                    "from": {
                        "email": "anotheremail@gmail.com"
                    },
                    "content": [
                        {
                            "type": "text/plain",
                            "value":  formatEmail //variable with the email text
                        }
                    ]
                }
            }

            $.ajax(settings).then(
                function success(response, status) {
                    dialog.close();
                    if (status == 202){
                        MessageBox.confirm('Mensagem enviada com sucesso', {
                            actions: [sap.m.MessageBox.Action.OK],
                            onClose: function(sAction){
                                that.onNavBack()
                            }
                        });
                    } else {
                        MessageBox.error('Erro!');
                    }
                },

                function fail(data, status){
                    dialog.close();
                    MessageBox.error('Request failed.  Returned status of ' + status + ' DEBUGDATA: ' + JSON.stringify(data));
                }  
            );

我真的不知道發生這種情況的原因,因為Postman生成的代碼段與我的代碼非常相似。 任何幫助將不勝感激!

提前致謝

通常,HTTP 400表示請求錯誤,這意味着您將錯誤的參數傳遞給HTTP POST請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM