How to get JSON information from ASP.NET core web app using JavaScript

故事扮演 提交于 2019-12-11 19:36:38

问题


I've been using eBay finding API for some time, it worked at as it supposed to, I've implemented it with JavaScript like shown in they'r documentation. and recently, out of nowhere it stopped working, I have no Idea whats the problem..

I checked same call using postman, everything works there. Also it works when I make same call in php or in C#, So I thought maybe eBay did something and I can only get info from "backend" language :/? IDK.. So I've created a link that gets info from eBay ant outputs it with get call and when I trie to get that data using Javascript it also fails, this link also works in postman or browser.

Can someone explain to me what's happening here, is something wrong with my code ? (it worked before with no problems for more than half year..), then it must be something with eBay or IDK.. and also why my link dosnt work, I'll show you all code, and errors using browser debug mode.

okay So my JavaScript code:

var urlp1 = "https://svcs.ebay.com/services/search/FindingService/v1";
    urlp1 += "?OPERATION-NAME=findItemsByKeywords";
    urlp1 += "&SERVICE-VERSION=1.0.0";
    urlp1 += "&SECURITY-APPNAME=myappname";
    urlp1 += "&GLOBAL-ID=EBAY-GB";
    urlp1 += "&RESPONSE-DATA-FORMAT=JSON";
    urlp1 += "&callback=pages1";
    urlp1 += "&REST-PAYLOAD";
    urlp1 += "&keywords=" + zodis;
    urlp1 += "&outputSelector(0)=PictureURLLarge";
    urlp1 += "&outputSelector(1)=StoreInfo";
    urlp1 += "&paginationInput.entriesPerPage=" + EPP;
    urlp1 += "&paginationInput.pageNumber=" + puslapis;
    urlp1 += "&itemFilter(0).name=Seller";
    urlp1 += "&itemFilter(0).value(0)=daviva16";
    urlp1 += "&itemFilter(0).value(1)=davivaltd";
    urlp1 += "&itemFilter(0).value(2)=davivastore";


ss=document.createElement('script'); 
ss.src= urlp1;
document.body.appendChild(ss);

Callback function is 100% OK, the http call itself fails, I get :

(response body is empty)

but if I use same link (minus callback) (I paste link into browser) I get expected info:

So my first approach was to somehow fix this problem.. But I have no Idea what to do about it I've tried to make call using XMLHttpRequest and JQuery Ajax no success either way..

Then I made this link with asp.net core 2.1 C#

[HttpGet]
        [ProducesResponseType(StatusCodes.Status201Created)]
        [ProducesResponseType(StatusCodes.Status400BadRequest)]
        [Route("Public/Ebaycalaidaviva162")]        
        public ActionResult<JObject> Ebaycalaidaviva162(string zodis, string kiekis, string puslapis)
        {
            string atsakas = "";    

            using (HttpClient client = new HttpClient())
            {

                try
                {
                    //Post http callas.
                    HttpResponseMessage response = client.GetAsync("https://svcs.ebay.com/services/search/FindingService/v1?"
                         + "OPERATION-NAME=findItemsIneBayStores"
                         + "&SERVICE-VERSION=1.0.0"
                         + "&SECURITY-APPNAME=myappname"
                         + "&GLOBAL-ID=EBAY-GB"
                         + "&storeName=daviva16"
                         + "&RESPONSE-DATA-FORMAT=JSON"
                         + "&outputSelector(0)=PictureURLLarge"
                         + "&outputSelector(1)=StoreInfo"
                         + "&keywords=" + zodis
                         + "&paginationInput.entriesPerPage=" + kiekis
                         + "&paginationInput.pageNumber=" + puslapis).Result;
                    //nesekmes atveju error..
                    response.EnsureSuccessStatusCode();
                    //responsas to string
                    string responseBody = response.Content.ReadAsStringAsync().Result;                   
                    atsakas = responseBody;

                }
                catch (HttpRequestException e)
                {
                    Console.WriteLine("\nException Caught!");
                    Console.WriteLine("Message :{0} ", e.Message);
                }

            }
            return JObject.Parse(atsakas);
        }

And I cannot get info from this link also..

Status is OK, but body is empty, and when I open this link in browser once again i get all the info..

Link is: https://backend.daviva.lt/public/Ebaycalaidaviva162?zodis=part&kiekis=12&puslapis=1 this link is up for now, and it has 5k request limit per day, so u can test this out if you wanna..

I think this problem might have very simple solution, but I myself lack knowledge to solve it..

It does not matter to me how you solve it You can change something in APS.NET Core link so it works with JavaScript call or change JavaScript call, or whatever.. All that matters to me so I can get Json data with javaScript..

Thanks !

To Slim

I'v tried this:

$.ajax({
async: true,
type: "GET",    
dataType: "json",
url: "https://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&SECURITY-APPNAME=myappname&GLOBAL-ID=EBAY-GB&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD&keywords=part&outputSelector(0)=PictureURLLarge&outputSelector(1)=StoreInfo&paginationInput.entriesPerPage=8&paginationInput.pageNumber=1&itemFilter(0).name=Seller&itemFilter(0).value(0)=daviva16&itemFilter(0).value(1)=davivaltd&itemFilter(0).value(2)=davivastore",
crossDomain: true,
success: function (atsakas) {
    alert(atsakas);
},
error: function (error) {
    alert("error  " + error);
}

And I get Status Code 200, but response body is empty and It triggers error in ajax function. Response headers:

And I get CORS error in console..

SEC7120: [CORS] The origin 'file://' did not find 'file://' in the Access-Control-Allow-Origin response header for cross-origin resource at 'https://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&SECURITY-APPNAME=myappname&GLOBAL-ID=EBAY-GB&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD&keywords=part&outputSelector(0)=PictureURLLarge&outputSelector(1)=StoreInfo&paginationInput.entriesPerPage=8&paginationInput.pageNumber=1&itemFilter(0).name=Seller&itemFilter(0).value(0)=daviva16&itemFilter(0).value(1)=davivaltd&itemFilter(0).value(2)=davivastore'.


回答1:


Sorry for my first answer. The reason you are receiving an empty response is related to the cross origin header missing from server side. The response from the server must include "Access-Control-Allow-Origin" header. You may test below url it includes this header and it will work with ajax.

http://api.geonames.org/countryInfoJSON?formatted=true&lang=en&country=US&username=webucator&style=full



来源:https://stackoverflow.com/questions/58167769/how-to-get-json-information-from-asp-net-core-web-app-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!