Problems, need help? Have a tip or advice? Post it here.
14 posts Page 1 of 2
Hello All and KK Sir,
I am trying to hit an API, but I keep getting the error:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://staging-express.delhivery.com/c ... des=440025. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

Is there a way Couch can help in this?

The request is being sent to the API using an AJAX Call.

Regards,
GenXCoders
Image
where innovation meets technology
Hi GenXCoders,

the http response header for the url is set to:
Code: Select all
X-Frame-Options: SAMEORIGIN

You must allow the access for the query url, see:
https://developer.mozilla.org/de/docs/W ... me-Options

If you don't have access to the web server configuration, you can try it with php:
Code: Select all
<cms:php>header("X-Frame-Options: allow-from https://example.com/"); </cms:php>


or via htaccess:
Code: Select all
Header set X-Frame-Options "ALLOW-FROM https://example.com/"


If you make the API call from the domain, check if you use www like https://www.staging-express.delhivery.com/

Hope, this helps.
Thanks for the response Bro Simon.
Where do you suggest i should place the code:
Code: Select all
<cms:php>header("X-Frame-Options: allow-from https://example.com/"); </cms:php>


I have a checkout.php which happens to be the best location for hitting the API. I placed it there, but still the same issue. This issue I am getting at two places (from localhost as well as server):
1. Delhivery API (https://staging-express.delhivery.com/c ... des=440025)
2. Razorpay API (https://api.razorpay.com/v1/orders)

In razorpay it goes ahead and asks me to share a api key, which after sharing also shows NS_ERROR_BAD_URI.

I suppose the CORS Access-Control-Allow-Origin issue should solve both issues.
Could you please provide me with some detail on where to include the X-Frame-Options or Access-Control-Allow-Origin?

Regards,
GenXCoders (Aashish)
Image
where innovation meets technology
genxcoders wrote: Could you please provide me with some detail on where to include the X-Frame-Options or Access-Control-Allow-Origin?


Hi Aashish,

sorry for answering so late, I am only randomly here. I would set it as soon as possible. The config.php should be the best option. You can check it in chrome, if it works:

https://mkyong.com/computer-tips/how-to ... le-chrome/

Greetings from germany
Bertram
Greetings Bro Bertram,

Thanks for the resource you pointed me to. It was helpful in gaining knowledge.
I discussed with the tech support team of the Vendor. They suggested using cURL to handle the issue. I am currently trying that out. If you have a good resource on the same that you could point me to, I would be thanksful as always!

Regards,
Aashish
Image
where innovation meets technology
Aashish, there was a recently updated post by @mwlarkin1 about cURL with a couch tag to make it easier. viewtopic.php?f=8&t=12953
@Bro Anton,

Thanks for pointing me to the resource, I have taken the essentials from that and implemented it as follows:
Code: Select all
<cms:curl 
    url='https://staging-express.delhivery.com/c/api/pin-codes/json/?filter_codes=440027'
    headers="Content-type: application/json | Authorization: Token 43cd5ff60f9cc9b41c7814f3d1911b03f8863583"
    method='get'
    into='response'
    is_json='1'
/>
<cms:show response as_json='1' />


Works till this point.

The json returned on execution is:
{
"delivery_codes":[
{
"postal_code":{
"repl":"Y"
"inc":"Nagpur_PC (Maharashtra)"
"center":[...
]
"pin":440032
"max_amount":0
"pre_paid":"Y"
"cash":"Y"
"max_weight":0
"pickup":"Y"
"district":"Nagpur"
"covid_zone":NULL
"country_code":"IN"
"is_oda":"N"
"sort_code":"NAG/SWI"
"state_code":"MH"
"cod":"Y"
}
}]
}


Now when I do:
Code: Select all
<cms:show response.delivery_codes as_json='1' />

It still works.

What I am unable to understand is how can I access a specific value. Say for example:
If is want to access "district" I tried the following:
Code: Select all
<cms:show response.delivery_codes.postal_code.pin as_json='1' />

But i get the error message:
SyntaxError: JSON.parse: unexpected end of data at line 4 column 1 of the JSON data


How can I access a particular key:value pair?

Regards,
Aashish
Image
where innovation meets technology
Hi Aashish,

Glad to see the <cms:curl /> tag in action :)

It looks like response.delivery_codes is an array (not an object). So you'll either need to loop through that array, or, if you know the item you need, use its index.

Code: Select all
<cms:show response.delivery_codes.0.postal_code.pin />
440027

<cms:each response.delivery_codes as='delivery_code'>
  <cms:show delivery_code.postal_code.pin /> // no need to pass as_json='1' here since the 'pin' value itself isn't a Couch array
</cms:each>
440027


Code: Select all
//Response as pretty JSON (https://jsonformatter.curiousconcept.com)

{
  "delivery_codes": [
    {
      "postal_code": {
        "repl": "Y",
        "inc": "Nagpur_PC (Maharashtra)",
        "center": [
          {
            "code": "IND440024AAA",
            "e": "2016-01-21T11:26:46.425",
            "cn": "Nagpur_RingRd_DC (Maharashtra)",
            "s": "2016-01-08T13:47:44.439",
            "u": "Aayush.Agarwal",
            "ud": "2016-01-08T13:47:44.439",
            "sort_code": "NAG/SWI"
          },
          {
            "code": "IND440025AAB",
            "cn": "Nagpur (Maharashtra)",
            "s": "2016-01-21T11:26:46.425",
            "u": "Aayush.Agarwal",
            "ud": "2016-01-21T11:26:46.425",
            "sort_code": "NAG/SWI"
          }
        ],
        "pin": 440027,
        "max_amount": 0,
        "pre_paid": "Y",
        "cash": "Y",
        "max_weight": 0,
        "pickup": "Y",
        "district": "Nagpur",
        "covid_zone": null,
        "country_code": "IN",
        "is_oda": "N",
        "sort_code": "NAG/SWI",
        "state_code": "MH",
        "cod": "Y"
      }
    }
  ]
}
any ideas why i cannot pass a couch show tag into the <cms:curl/> tag under filters

Code: Select all
<cms:curl
        url='https://pim.plytix.com/api/v1/products/search'
        method='POST'
        headers="Content-Type: application/json | Authorization: Bearer <cms:show response.data.0.access_token/>"
        data='{"filters": [
            [
                {"value": "<cms:show selected_product/>", "field": "sku", "operator": "eq"}
            ]
          ],
         "attributes": ["attributes.cust_price"]   
            }'
        into='products'
        is_json='1'
        />

   
<cms:show products as_json='1'/>      
      
@rudydjmedina
The 'data' param should use double-quotes if there are Couch tags in the enclosed contents (please see https://docs.couchcms.com/concepts/sett ... eters.html if you need more details about the importance of which quotes to use) i.e.
Code: Select all
data=" ... "

Your modified code could be this -
Code: Select all
data="{'filters': [ 
            [
                {'value': '<cms:show selected_product/>', 'field': 'sku', 'operator': 'eq'}
            ]
          ],
         'attributes': ['attributes.cust_price']   
      }"

Hope this helps.
14 posts Page 1 of 2
cron