简体   繁体   English

ajax中的RESTful请求并做出反应

[英]RESTful request in ajax and react

good day, im trying to consume a web service in react, but im having problems with the ajax function, i'm not sure if is working my code is this: 美好的一天,我试图在反应中消耗Web服务,但是我在ajax函数方面遇到问题,我不确定我的代码是否在工作:

prox= {"email":email, "password": password};
//tag comment
$.ajax({
  type: 'POST',
  url: (web service url),
  data: JSON.stringify(prox),
  contentType: "application/json; charset=utf-8",
  crossDomain: true,
  dataType: 'json',   
  success: function(data) {
    this.setState({success: true, result: data});
    alert("success");
    this.setState({prueba: 'success'});
  }.bind(this),
  error: function() {
    this.setState({failure: true});
    alert("failure");
    this.setState({prueba: 'failure'});
  }.bind(this)
});

but i dont have any alert, when i click the button only re-render the form, the function handdle submit works, i try it putting a confirm() in the space where the //tag comment is and the confirm pop up, but the alerts dont, i think that i have an error in the function or something, thank's for the help. 但是我没有任何警报,当我单击按钮仅重新呈现表单时,函数句柄提交工作正常,我尝试将confirm()放在// tag注释所在的空间中,并弹出确认,但是警报没有,我认为我在功能或某些方面有错误,谢谢您的帮助。

I didn't run the script but just looking at it I imagine the problem could be your bind(this) 我没有运行脚本,而是看着它,我想问题可能出在您的bind(this)

this.setState to me should be an error “ is not a function ” as this is not the react object. 对我来说this.setState应该是一个错误“ 不是函数 ”,因为this不是反应对象。 To get an alert try placing the alert as the first state. 要获取警报,请尝试将警报设置为第一状态。

To be sure just look at your browser console. 可以肯定的是,请查看您的浏览器控制台。

It looks like its working for the most part. 看起来大部分时间都在工作。 I threw it into a JSBin and nothing seems to be out of the ordinary. 我将其放入一个JSBin中,似乎没有什么异常。

http://jsbin.com/rulaguxote/1/edit?html,js,output http://jsbin.com/rulaguxote/1/edit?html,js,输出

I kept your JSX mostly the same, and added a few things in the component to help you visualize its state. 我使您的JSX基本保持不变,并在组件中添加了一些内容来帮助您可视化其状态。 Click the button to send a fake ajax request. 单击按钮发送伪造的ajax请求。 Depending on the state, it will either send back an HTTP status of 200 or 400 (success or failure). 根据状态,它将发送回200或400的HTTP状态(成功或失败)。 This way, you can see how the success() and error() functions behave. 这样,您可以看到success()error()函数的行为。

Another thing to note: If you are concerned that your .bind(this) is the reason your code is not working, you can specify the context like this: 需要注意的另一件事:如果您担心.bind(this)是代码无法正常工作的原因,则可以指定如下上下文:

$.ajax({
    type        : 'POST',
    url         : '/test',
    data        : JSON.stringify(prox),
    contentType : "application/json; charset=utf-8",
    context     : this, //use this instead of .bind
    success     : function (data) {
        this.setState({success : true, failure : false});
        alert("success");
    },
    error       : function () {
        this.setState({failure : true, success : false});
        alert("failure");
    }
});

Let me know if you have any questions. 如果您有任何疑问,请告诉我。

经过大量研究,我发现我的真正问题出在同一来源策略上,现在在项目的localhost版本中可以正常工作,谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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