简体   繁体   中英

Http.post() AJAX no response from JSP/Spring server

In my Ionic 2 app, I'm fetching data from example site www.my-website.com/member/loginPersist.do using this block of code :

This /member/loginPersist is a Spring MVC controller

login-service.ts

 private GetUser(user : User): Promise<any> {
    let options = this.makeHeader("post");
    let x = this.http.post("http://my-website.com/member/loginPersist.do", this.serialize(user) ,options).toPromise().catch(this.handleError);

    return x;

  }
private makeHeader(method : string){
  let headers = new Headers({
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'X-Requested-With': 'XMLHttpRequest'

  });
  let options = new RequestOptions({ headers: headers });

  return options;
}

But I get no response / Blank(no json results) and status code is 200 OK. But the response is empty .

But If I change the url to www.site.com/test.php I get a response from a PHP . So I know that my http request code is not the problem.

Is this problem related to the JSP/Spring server? and How would I get the JSON response

My guess based on the information you provided is that you are having cross origin request issues. Please make sure in your controller you have allowed cross origin requests for the method. Just anotate the method with

@CrossOrigin

This will basically indicate to your server that should respond to the browser that it accepts requests from any domain. You can also restrict the domains you want to allow. More here http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cors.html

If you don't enable this on the server the browser will not complete the request due to security policies.

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