1

my json file like this

[{
 "code": "Free",
 "type": "base",
 "name": "Start",
 "price": "Free",
 "subscriptionunit": "month",
 "subscriptionqty": 1,
 "customizable": false

}]

This json file in separate server and my web server another server I coded like this to get json detail from same same server and same project.

$http({
                method: 'GET',
                url: './json/priceplan.json',
                dataType: "json",
                headers: {
                    "Access-Control-Allow-Origin": "*",
                    "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
                    "Access-Control-Allow-Headers": "Content-Type, X-Requested-With",
                    "Content-Type": "text/json"
                },

            })
                .success(function (data) {
                    console.log("Ok : " + data);                       

                })
                .error(function (data) {    
                    console.log("Error : " + data);
                });

I want to know how retrieve json file in separate server.

Lakmi
  • 1,379
  • 3
  • 16
  • 27

2 Answers2

0

You're trying to set CORS headers client-side. That doesn't work, those headers are required to be in the response from the server, not the request from the client.

You'll have to modify the other server to enable CORS from the origin you're making the request from. This is a server-side thing.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I already enable CORS other server.but I didn't get any result – Lakmi May 02 '17 at 06:44
  • @brk: Yes, but then the thing being fetched isn't a JSON file anymore. – T.J. Crowder May 02 '17 at 06:44
  • @Lakmi: Then you didn't enable it correctly. Browsers aren't broken, CORS does work. But remove those headers from the request. CORS is header-sensitive. Just do your request normally, and then adjust the server-sent headers accordingly. – T.J. Crowder May 02 '17 at 06:44
  • @Crowder: ok i'll try it again thnx – Lakmi May 02 '17 at 06:47
  • @Crowder : when I try to test local host i got error like this : Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. – Lakmi May 02 '17 at 07:19
  • @Lakmi: Which tells you you're not sending the appropriate headers from the server. It's worth having a really thorough read of the spec linked above. Note that the error you've quoted says "preflight" -- perhaps you're not handling the `OPTIONS` request? Just `GET`? – T.J. Crowder May 02 '17 at 07:29
  • @Crowder : i send header like this : headers: { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", "Access-Control-Allow-Headers": "Content-Type, X-Requested-With", "Content-Type": "text/json" }, – Lakmi May 02 '17 at 07:49
  • @Lakmi: "Handling the OPTIONS request" means just that: Processing a request from the client with the HTTP verb OPTIONS. Again: Read the spec. – T.J. Crowder May 02 '17 at 08:06
  • i fixed it ,i copied json file to same server now it's work thnx @Crowder – Lakmi May 02 '17 at 09:57
-2

if other server is in internet, then you will have to host website (allow MIME type json on iis) on that server with some domain name e.g. www.xyz.com.

in publish folder of www.xyz.com site store your json and access it with URL e.g. www.xyz.com/file/test.json

else if both servers on same server, then you dont have to buy domain

  • this is not related to this question.i have two separate server web and service i want to get json file details in server to web server. – Lakmi May 02 '17 at 06:50