Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Answered

JSON Ajax request throwing error

Posted on by 680

I keep getting the error 'SCRIPT1004: SCRIPT1004: Expected ';'' on the returned result.

Here is my code:

$.ajax({
type: "POST",
url: "">npiregistry.cms.hhs.gov/.../ + NPI + "",
dataType: "jsonp",
jsonpCallback: 'processJSONPResponse', // add this property
contentType: "application/json; charset=utf-8",
success: function(data) {
console.log(data);
},
error: function(data){
console.log(data);
}
});

I tried changing the method to GET and Changed the data type to JSON then it would throw CORS issue. Please suggest. Thanks.

  • Karthik Dayala Profile Picture
    Karthik Dayala 680 on at
    RE: JSON Ajax request throwing error

    Thank you.

  • Verified answer
    cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: JSON Ajax request throwing error

    Hi Karthik,

    Because the website "https://npiregistry.cms.hhs.gov/" has blocked CORS request, so you need a intermediate server to help you handle request, 

    which means it will send request to hhs.gov, then the server will forward result to you. (like a Proxy)  

    There are some serverless solutions to meet your requirement.

    1. Free public CORS forward service:

    https://cors-anywhere.herokuapp.com/

    https://github.com/netnr/workershttps://cors.zme.ink/ * )

    Usage:

    url: serverUrl/apiEndpoint

    Here is my demo, run the request by clicking a button.

    $.ajax({
    
        type: 'GET',
    
        url: "https://cors-anywhere.herokuapp.com/https://npiregistry.cms.hhs.gov/api/?version=2.0&number=1316459605",
    
        // url: "https://cors.zme.ink/https://npiregistry.cms.hhs.gov/api/?version=2.0&number=1316459605",
    
        // only required when using the first url 
        beforeSend: function(request) {
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
          },
    
        success: function (data) {
            console.log(data.results[0]);
        },
    
        error: function (data) {
            alert('failed');
        }
    
    });
     

    result:

    pastedimage1585892540538v3.png

    2. Create your own proxy server with Cloudflare Worker service, the second service(*) is built on it.

    https://workers.cloudflare.com/

    demo project:

    https://github.com/Zibri/cloudflare-cors-anywhere

    Regards,

    Clofly

  • Karthik Dayala Profile Picture
    Karthik Dayala 680 on at
    RE: JSON Ajax request throwing error

    this is on portal field on change event.

  • Karthik Dayala Profile Picture
    Karthik Dayala 680 on at
    RE: JSON Ajax request throwing error

    I do get CORS issue when I use the dataType json hence I switched it to jsonp. I'm trying this on a custom portal field on change event. The funny thing is my block of code executes but enters error with the status code 200.

    Here is my code:

    function getNPPESData() {

       var NPI = $('#nact_npi').val();

    if (NPI != null) {    

    $.ajax({

           type: 'GET',

                   url:      "npiregistry.cms.hhs.gov/.../ + NPI + "",

                   dataType: 'jsonp',

                   success: function(data) {

                      alert('success');

                   },

    error: function(data){

    alert('failed');

    }

      });

    }

    }

    The error on console when I debug:

    SCRIPT1004: SCRIPT1004: Expected ';'

    api (1,16)

    Here is my api response:

    {"result_count":1, "results":[{"enumeration_type": "NPI-1", "number": 1316459605, "last_updated_epoch": 1508976000, "created_epoch": 1508976000, "basic": {"first_name": "BIANCA", "last_name": "AARON", "credential": "MFTI, PCCI", "sole_proprietor": "NO", "gender": "F", "enumeration_date": "2017-10-26", "last_updated": "2017-10-26", "status": "A", "name": "AARON BIANCA"}, "other_names": [], "addresses": [{"country_code": "US", "country_name": "United States", "address_purpose": "LOCATION", "address_type": "DOM", "address_1": "1540 E COLORADO ST", "address_2": "", "city": "GLENDALE", "state": "CA", "postal_code": "912051514", "telephone_number": "818-244-7257", "fax_number": "818-243-5431"}, {"country_code": "US", "country_name": "United States", "address_purpose": "MAILING", "address_type": "DOM", "address_1": "4760 SEPULVEDA BLVD", "address_2": "", "city": "CULVER CITY", "state": "CA", "postal_code": "902304820", "telephone_number": "310-390-6612", "fax_number": "310-398-5690"}], "taxonomies": [{"code": "106H00000X", "desc": "Marriage & Family Therapist", "primary": true, "state": "", "license": ""}], "identifiers": []}]}

  • Suggested answer
    cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: JSON Ajax request throwing error

    Hi Karthik,

    Could you share how would you like to integrate data from https://npiregistry.cms.hhs.gov/ within your Dynamics 365 environment?

    E.g: display them in web resource or  a web page of Portal? 

    The error told us that npiregistry has blocked XMLHttpRequest request from other websites/domains except itself.

    Please find whether there is setting in npiregistry to allow your website or domain to access its data.

    If you can't find such setting, it means that we couldn't get data from front-end, thus you need to get data with back-end method.

    You could use HTTP connector to request API(back-end way) and read its JSON body with other connectors without building a web application.

    e.g: Get Baltimore data from npiregistry.

     pastedimage1585821856851v1.png

    Regards,

    Clofly

  • oliver.rodrigues Profile Picture
    oliver.rodrigues 4,052 on at
    RE: JSON Ajax request throwing error

    is this related to portals or crm?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans