如何从React前端发送POST请求到PHP后端?

时间:2017-12-13 05:41:30

标签: php reactjs

我会从反应前端发送POST请求到php后端。 这是代码。

- 前端

fetch(backendUrl + "/list", {
    method: "POST",
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(filterData)
})
.then(function(res) {
    return res.json()
})
.then(function(data) {
    self.setState({
        OddList: data.list
    });
    self.props.setPageCount(data.page_count);
    self.props.onShowLoading(false);
})

1 个答案:

答案 0 :(得分:1)

评论中提到您使用的PHP API具有不同的域。我认为问题在那里。

如果您使用这种方式,请求将类似于以下URL

NSString *imageValue = @"btnCamera.png";
UIImage *cameraImage = [UIImage imageNamed:imageValue];
UIButton *cameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
cameraButton.bounds = CGRectMake(0, 0, 30, 49);
[cameraButton setImage:cameraImage forState:UIControlStateNormal];
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithCustomView:cameraButton];
[cameraButton addTarget:self
                  action:@selector(inputCamButtonPressed)
        forControlEvents:UIControlEventTouchUpInside];

NSMutableArray *items = [[NSMutableArray alloc] init];

UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
spacer.width = -12;
[items addObject:spacer];
[items addObject:cameraItem];

UIBarButtonItem *fixedItemSpaceWidth = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

fixedItemSpaceWidth.width = screenWidth - 90; 
[items addObject:fixedItemSpaceWidth];

imageValue = @"send.png";
UIImage *sendImage = [UIImage imageNamed:imageValue];
UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
sendButton.enabled = NO;
sendButton.tintColor = [UIColor blackColor];
sendButton.bounds = CGRectMake(0, 0, 30, 49);
[sendButton setImage:sendImage forState:UIControlStateNormal];
UIBarButtonItem *sendItem = [[UIBarButtonItem alloc] initWithCustomView:sendButton];
sendItem.tintColor = [UIColor blackColor];

/* Create UIExpandingTextView input */
self.textView = [[BHExpandingTextView alloc] initWithFrame:CGRectMake(40, 7, self.bounds.size.width - 70, 26)];
self.textView.internalTextView.scrollIndicatorInsets = UIEdgeInsetsMake(4.0f, 0.0f, 10.0f, 0.0f);
self.textView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
self.textView.delegate = self;
[self addSubview:self.textView];

[self setItems:items animated:NO];

虽然你实际上想要点击类似于下面的URL

http://frontendappdomain/backendurl/list

尝试发送完整的网址而不是"路径名"。如果我正确理解你的问题,这将解决你的问题。

相关问题