Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How create a cross domain web api?

How create a cross domain web api?

Scheduled Pinned Locked Moved ASP.NET
jsondatabasehelpcomhosting
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    miss786
    wrote on last edited by
    #1

    I am trying to create cross domain web api, however I am still unable to call data from web api url, in my ajax function. I am currently experiencing the following syntax error, under the networks, response tag. **SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data** The ’/ api/stw’ is currently being reported as 405 error, however when I call - http://apitesting.domain.com/api/stw, I am able to see the data is JSON format. So, I am little unsure, why is it failing in the ajax function.

    $(document).ready(function () {
    $.support.cors = true;
    $.ajax({
    type: "GET",
    crossDomain: true,
    contentType: "application/json; charset=utf-8",
    // url: "/api/stw",
    url: "http://apitesting.domain.com/api/stw,",
    data: "{}",
    dataType: "json",
    success: function (data) {
    console.log(data);
    // var myData = JSON.parse(data)
    for (var i = 0; i < data.length; i++) {
    $("#tbDetails").append("" + data[i].Name + "" + data[i].loan + "" + data[i].evnt + "");
    }
    },
    error: function (result) {
    alert("Error");
    }
    });
    });

    I am currently hosting my cross domain web api on the above url. I have added the custom headers in the web.config file of my web api. I have also added ‘enabled.cors’ property in my stwAPIController. I tried parsing the data in ajax through the following method ‘JSON.parse(data)’,but I am still getting the same error, as mentioned above.

    [EnableCors(origins: "http://apitesting.domain.com/api/stw", headers: "*", methods: "*")]
    public class STWController : ApiController
    {
    private cdwEntities db = new cdwEntities();

        public IEnumerable getData()
        {
    
            var data = db.Database\_CRE\_LoanEvents.Where(c => c.Date.Contains("2015") && c.Loan\_property != null)
                          .Select(x => new Loan() { Name = x.Deal, loan = x.Loan\_property, evnt = x.Event })
                          .ToList().Take(3);
            return data;
        }
    }
    

    Any further help, would be very much appreciated.

    A J 2 Replies Last reply
    0
    • M miss786

      I am trying to create cross domain web api, however I am still unable to call data from web api url, in my ajax function. I am currently experiencing the following syntax error, under the networks, response tag. **SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data** The ’/ api/stw’ is currently being reported as 405 error, however when I call - http://apitesting.domain.com/api/stw, I am able to see the data is JSON format. So, I am little unsure, why is it failing in the ajax function.

      $(document).ready(function () {
      $.support.cors = true;
      $.ajax({
      type: "GET",
      crossDomain: true,
      contentType: "application/json; charset=utf-8",
      // url: "/api/stw",
      url: "http://apitesting.domain.com/api/stw,",
      data: "{}",
      dataType: "json",
      success: function (data) {
      console.log(data);
      // var myData = JSON.parse(data)
      for (var i = 0; i < data.length; i++) {
      $("#tbDetails").append("" + data[i].Name + "" + data[i].loan + "" + data[i].evnt + "");
      }
      },
      error: function (result) {
      alert("Error");
      }
      });
      });

      I am currently hosting my cross domain web api on the above url. I have added the custom headers in the web.config file of my web api. I have also added ‘enabled.cors’ property in my stwAPIController. I tried parsing the data in ajax through the following method ‘JSON.parse(data)’,but I am still getting the same error, as mentioned above.

      [EnableCors(origins: "http://apitesting.domain.com/api/stw", headers: "*", methods: "*")]
      public class STWController : ApiController
      {
      private cdwEntities db = new cdwEntities();

          public IEnumerable getData()
          {
      
              var data = db.Database\_CRE\_LoanEvents.Where(c => c.Date.Contains("2015") && c.Loan\_property != null)
                            .Select(x => new Loan() { Name = x.Deal, loan = x.Loan\_property, evnt = x.Event })
                            .ToList().Take(3);
              return data;
          }
      }
      

      Any further help, would be very much appreciated.

      A Offline
      A Offline
      Afzaal Ahmad Zeeshan
      wrote on last edited by
      #2

      Your JSON parser says, that the token wasn't expected. This happens either when you're trying to parse array to object or object to array. Please make sure types match. Also what do you mean by cross domain? Do you stumble upon CORS?

      The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

      1 Reply Last reply
      0
      • M miss786

        I am trying to create cross domain web api, however I am still unable to call data from web api url, in my ajax function. I am currently experiencing the following syntax error, under the networks, response tag. **SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data** The ’/ api/stw’ is currently being reported as 405 error, however when I call - http://apitesting.domain.com/api/stw, I am able to see the data is JSON format. So, I am little unsure, why is it failing in the ajax function.

        $(document).ready(function () {
        $.support.cors = true;
        $.ajax({
        type: "GET",
        crossDomain: true,
        contentType: "application/json; charset=utf-8",
        // url: "/api/stw",
        url: "http://apitesting.domain.com/api/stw,",
        data: "{}",
        dataType: "json",
        success: function (data) {
        console.log(data);
        // var myData = JSON.parse(data)
        for (var i = 0; i < data.length; i++) {
        $("#tbDetails").append("" + data[i].Name + "" + data[i].loan + "" + data[i].evnt + "");
        }
        },
        error: function (result) {
        alert("Error");
        }
        });
        });

        I am currently hosting my cross domain web api on the above url. I have added the custom headers in the web.config file of my web api. I have also added ‘enabled.cors’ property in my stwAPIController. I tried parsing the data in ajax through the following method ‘JSON.parse(data)’,but I am still getting the same error, as mentioned above.

        [EnableCors(origins: "http://apitesting.domain.com/api/stw", headers: "*", methods: "*")]
        public class STWController : ApiController
        {
        private cdwEntities db = new cdwEntities();

            public IEnumerable getData()
            {
        
                var data = db.Database\_CRE\_LoanEvents.Where(c => c.Date.Contains("2015") && c.Loan\_property != null)
                              .Select(x => new Loan() { Name = x.Deal, loan = x.Loan\_property, evnt = x.Event })
                              .ToList().Take(3);
                return data;
            }
        }
        

        Any further help, would be very much appreciated.

        J Offline
        J Offline
        jkirkerx
        wrote on last edited by
        #3

        1. Well as far as I know, Javascript and jQuery doesn't allow you to do cross domain calls, to protect the website from false calls to web services within your web site. But on the flip side of the coin, I know you cal call card processing services from jquery, so I need to update my knowledge on that. 2. You need to check your script for errors on something like http://www.javascriptlint.com/online_lint.php[^] 3. With Javascript and JQuery, you really need to use the single quote char, and not the double quote char. by using the single quote char to wrap text values, you can use the double quote char without having to escape it e.g.

        var html = '

        ';

        I can't tell why it's not working, but you should code it correctly first before moving forward, you could just be getting the error after the first error.

        $(document).ready(function () {
        2 $.support.cors = true;
        3 $.ajax({
        4 type: "GET",
        5 crossDomain: true,
        6 contentType: "application/json; charset=utf-8",
        7 // url: "/api/stw",
        8 url: "http://apitesting.domain.com/api/stw,",
        ============================^
        SyntaxError: missing } after property list
        9 data: "{}",
        10 dataType: "json",
        11 success: function (data) {
        12 console.log(data);
        13 // var myData = JSON.parse(data)
        14 for (var i = 0; i < data.length; i++) {
        15 $("#tbDetails").append("" + data[i].Name + "" + data[i].loan + "" + data[i].evnt + "");
        16 }
        17 },
        18 error: function (result) {
        19 alert("Error");
        20 }
        21 });
        22 });

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups