简体   繁体   中英

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

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 '.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM