调用 AJAX 调用时出错

Error when invoking AJAX call

编辑

检查这里我编辑的 POST 关于 JSONP 问题我得到这个错误:

Failed to load resource: the server responded with a status of 500 (Internal Server Error).

     $.ajax({

  url: "https://brsv2-6855bc66.ng.bluemix.net/DecisionService/rest/vacationsRuleApp/1.0/vacationsRuleProject/1.0",

        // The name of the callback parameter, as specified by the YQL service
        jsonp: "callback",

        // Tell jQuery we're expecting JSONP
        dataType: "jsonp",

        // Tell YQL what we want and that we want JSON
        data: {
            "employeeID": "jujuju",
            "loanAmount": 10517320,
            "theEmployee": {
                "seniority": 3,
                "annualSalary": 10517320,
                "nbOfExtraVacationDaysBasedOnSeniority": 10517320
                    },
            "creditAmount": 20000,
            "__DecisionID__": "string",
            "AnnualSalary": 20000
                    },

        // Work with the response
        success: function( response ) {
            alert("Success");
            console.log( response ); // server response
        },
        fail: function(response){
            alert("Fail");
            alert(JSON.stringify(response));
        }
    });

默认情况下,浏览器不允许 javascript 调用另一个域 "brsv20cc90e37.ng.bluemix.net",除非 brsv20cc90e37 应用程序实现了 CORS - 基本上只有 header 在其响应中:

Access-Control-Allow-Origin: *

*号表示允许所有域调用。您也可以指定确切的域。

http://enable-cors.org/server.html

How does Access-Control-Allow-Origin header work?

如果您研究 "Access-Control-Allow-Origin"

,可以在线获得很多重要信息

在这种情况下,如果您不拥有 brsv20cc90e37 应用程序,则应使用服务器端代码而不是客户端 javascript 调用 brsv20cc90e37 API。一些 APIs,比如这个,不打算从浏览器调用,所以他们没有理由实现 CORS。

理想情况下,您应该在 Bluemix 的服务器端发出此请求,然后使用您的 Bluemix 应用程序将请求代理到您的客户端应用程序。您收到 CORS 错误是因为浏览器不允许您从不同域请求资源。

此外,业务规则服务似乎不允许 CORS。您将需要编写一些服务器端代码来代理您的请求。

我在下面发布了一些代码以及 Github 上的要点,以帮助您隔离问题。

抓取这两个文件并将它们放在一个目录中并修改 app.js 中具有业务规则的用户名和密码的 2 行。

package.json (https://gist.github.com/jsloyer/2bf436f342e1d24c3099)

{
  "name": "bluemix-business-rules",
  "version": "0.0.1",
  "dependencies": {
    "express": "~4.10.8",
    "restler": "~3.2.2",
    "async": "~0.9.0",
    "body-parser": "~1.12.4"
  },
  "engines": {
    "node": ">=0.10"
  },
  "author": "IBM Corp.",
  "contributors": [
    {
      "name": "Jeff Sloyer",
      "email": "jbsloyer@us.ibm.com"
    }
  ],
  "license": "Apache-2.0",
  "scripts": {
    "start": "node app.js"
  }
}

app.js (https://gist.github.com/jsloyer/e5a953cf5691a4aeefc2)

var app = require("express")(),
    restler = require("restler");


app.get("/", function(request, response) {
    var json = {
        "employeeID": "jujuju",
        "loanAmount": 10517320,
        "theEmployee": {
            "seniority": 3,
            "annualSalary": 10517320,
            "nbOfExtraVacationDaysBasedOnSeniority": 10517320
        },
        "creditAmount": 20000,
        "__DecisionID__": "string",
        "AnnualSalary": 20000
    };
    var options = {
        username: "replaceme",
        password: "replaceme"
    };
    var url = "https://brsv2-6855bc66.ng.bluemix.net/DecisionService/rest" + "/vacationsRuleApp/1.0/vacationsRuleProject/json";
    restler.postJson(url, json, options).on('complete', function(data) {
        response.send(data);
    });
});

app.listen(process.env.VCAP_APP_PORT || 8080);

然后 运行 cf push businessrules-vacationsRuleApp.

打开您的网络浏览器并导航到它为您提供的 url。希望我们可以尝试调试发生了什么。