Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

aurelia - How to resolve "TypeError: NetworkError when attempting to fetch resource."

When I use aurelia-fetch-client to post json data to server and I got this error "TypeError: NetworkError when attempting to fetch resource." I think your answer is very useful to me.

  • post.html

    <template>
    <section>
        <form role="form" submit.trigger="signup()">
        <div class="form-group">
            <label for="OrganisationId">OrganisationId</label>
            <input type="text" value.bind="organisationId" placeholder="OrganisationId">
        </div>
        <div >
            <label for="OrganisationName">OrganisationName</label>
            <input type="OrganisationName" value.bind="organisationName"  placeholder="Password">
        </div>
        <button type="submit" class="btn btn-default">Enter</button>
        </form>
    </section>
    </template>
    
  • post.js

    import 'fetch';
    import { HttpClient, json } from 'aurelia-fetch-client';
    
    let httpClient = new HttpClient();
    
    export class signup {
        heading1 = "Welome to User";
    
        organisationId = "";
        organisationName = "";
    
        signup() {
            alert("calliong");
    
            var myUser1 = { organisationId: this.organisationId, organisationName: this.organisationName }
            console.log(myUser1);
    
            httpClient.fetch('http://172.16.0.26:8085/employee-management/rest/employees/addOrganisations', {
                method: "POST",
                body: JSON.stringify(myUser1)
            })
                .then(response => response.json())
                .then(data => {
                    console.log(data);
                });
        }
    }
    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is probably related to Cross-Origin Resource Sharing (CORS).

The Cross-Origin Resource Sharing (CORS) mechanism gives web servers cross-domain access controls, which enable secure cross-domain data transfers. Modern browsers use CORS in an API container - such as XMLHttpRequest or Fetch - to mitigate risks of cross-origin HTTP requests. (Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

If you have Chrome you could try using Run in Windows with the command: chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security and see if you can run your code in that environment. This will allow access to no 'access-control-allow-origin' header requests.

I tried running parts of your code normally in Chrome, Firefox and Edge and got the same CORS errors. It did however run when I used the above command. You didn't give too much information to go on, but you might have to do some changes server side as well as in your code.

The command above and more good information about CORS can be found here on SO: "No 'Access-Control-Allow-Origin' header is present on the requested resource"

Hopefully this can at least point you in the right direction.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...