0

EDIT

CHEC HERE MY EDITED POST with the JSONP issue i get this error :

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));
        }
    });

2 Answers2

0

By default, a browser will not allow javascript to make a call to another domain "brsv20cc90e37.ng.bluemix.net", unless the brsv20cc90e37 application implemented CORS - basically just have this header in its response:

Access-Control-Allow-Origin: *

The * indicates that all domains are allowed to call this. You can also specify the exact domain instead.

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

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

Lots of great information available online if you research "Access-Control-Allow-Origin"

In this case, where you do not own the brsv20cc90e37 application, you should call the brsv20cc90e37 API using server side code instead of client side javascript. Some APIs, like this one, are not meant to be called from the browser, so there is no reason for them to implement CORS.

Community
  • 1
  • 1
Ram Vennam
  • 3,536
  • 1
  • 12
  • 19
0

You should ideally be making this requests server side in Bluemix and then using your Bluemix app to proxy the request to your client side app. You are getting the CORS error because browsers do not allow you to request resources from different domains.

Additionally the Business Rules service looks like it does not allow CORS. You will need to write some server side code to proxy your request.

I have have posted some code below as well as gists on Github to help you with isolating your issue.

Grab both of the files and put them in a directory and modify 2 lines in app.js that have the username and password for Business Rules.

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);

Then run cf push businessrules-vacationsRuleApp.

Open your web browser and navigate to the url it gives you. Hopefully we can try to debug whats going on.

Jeff Sloyer
  • 4,899
  • 1
  • 24
  • 48
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/84691/discussion-on-answer-by-jeff-sloyer-error-when-invoking-ajax-call). – Martijn Pieters Jul 30 '15 at 12:46