-5

Java script Code :

var AllTeams;

function GetAllTeams_ProductSamplePerClass() {
    return fetch("../Pages/Product.aspx/GetAllTeams", {
        method: 'POST',
        dataType: 'MyTeam'
    }
    ).then(response => {
        AllTeams = response.json();
        console.log('success getall teams', response);
        console.log('teams', AllTeams);
        return AllTeams;

    }).catch(error => {
        console.log('Error in getall teams', error);   
    })
}

C# Method in aspx file :

[WebMethod]
public static List<MyTeam> GetAllTeams()  
{
    TeamServiceClient TeamSvc = new TeamServiceClient("wsHttpBinding_TeamWsEndPoint");

    try
    {
        return TeamSvc.GetAllTeams();
    }
    catch
    {
        TeamSvc.Abort();
        return null;
    }
    finally
    {
        TeamSvc.Close();
    }
}
Rand Random
  • 7,300
  • 10
  • 40
  • 88
  • What exactly is the issue you're having? Are you getting an error? Unexpected results? Please describe the problem. – David Sep 17 '18 at 13:07
  • in dev tool get this in console success getall teams Response {type: "basic", url: "http://localhost:3737/Admin/Pages/Product.aspx/GetAllTeams", redirected: false, status: 200, ok: true, …} body : (...) bodyUsed : false headers : Headers {} ok : true redirected : false status : 200 statusText : "OK" type : "basic" url : "http://localhost:3737/Admin/Pages/Product.aspx/GetAllTeams" __proto__ : Response – Ibrahim Helmy Sep 17 '18 at 13:22
  • "Success" usually isn't an indication of a problem. Do you have an *actual problem* that you're observing somewhere? (One thing to note is that your `return` statement in your `then()` callback doesn't actually do anything, you don't need that.) – David Sep 17 '18 at 13:24
  • when i adding response.json() give me this error in console : teams Promise__proto__: Promise[[PromiseStatus]]: "rejected"[[PromiseValue]]: SyntaxError: Unexpected token < in JSON at position 4 – Ibrahim Helmy Sep 17 '18 at 13:30
  • Sounds like the response may not be JSON. In your browser's debugging tools, what is the actual response from the server? – David Sep 17 '18 at 13:32
  • That is way i ask , i hit break point in .net to see if fetch function call my c# method or not i found no access in c# method , this mean it's not call function and back with empty response that what is guess , so have you any idea about getting by fetch api from c# method ? – Ibrahim Helmy Sep 17 '18 at 13:36
  • So if it's not reaching your server-side code, what *is* it doing? Use your browser's debugging tools. Specifically on the network tab. Is the HTTP request what you expect it to be? What is the server's response? Is the response an error? What is that error? – David Sep 17 '18 at 13:38
  • now i checked from network tab, and i found the requerst Request URL: http://localhost:3737/Admin/Pages/Product.aspx/GetAllTeams Request Method: POST Status Code: 200 OK , but the response tab give me all aspx page that is not what i expect , i need only data from web method name GetAllTeams i used to get that by xhr = $.ajax but it's not the same for fetch – Ibrahim Helmy Sep 17 '18 at 14:40

1 Answers1

0

After search one day and keep trying and test , i found the issue that made fetch api not reach to the server is this line was not in javascript headers: { 'Accept': 'application/json', 'Content-type': 'application/json; charset=utf-8' } this made the response not html response as before but made the response object that came from web method thanks @David for your help the link i searched and found the solve : Fetch API call causes new Asp.net session