Convert php to javascript dan ajax

hello can somebody help me to convert this code to Javascript and ajax. Or can somebody show me how to read the URL?

$productcode = ‘123434’;
$post_data = array(
‘productcode’ => json_encode($productcode)
‘token’ => ‘mytokencode21asda3qastq2a’,
);
$api_url=‘https://api.sales.com/v2/product/index’;
$curl = curl_init($api_url);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$result = curl_exec($curl);
curl_close($curl);

$string = json_encode($result);
$decodedArray = json_decode($result,true);

We need a bit more information. I’m not quite understanding what you need.

If you have a web page and want to add some JS so that when you click something on the page it sends an ajax request directly from the browser to https://api.sales.com/v2/product/index without having to go through your php code on your server then just google “jquery ajax example” and what you find should be simple and should work. Hopefully all those SSL things will be handled properly by the web browser by default.

I’m trying to use this code, is this the correct way?

fetch(https://api.sales.com/v2/product/index, {
method: ‘POST’,
withCredentials: true,
credentials: ‘include’,
headers: {
‘Authorization’: bearer,
‘Content-Type’: ‘application/json’
‘token’:‘mytokencode21asda3qastq2a’,
‘productcode’:‘123434’’
}
}).then(responseJson => {
var items = JSON.parse(responseJson._bodyInit);
})
.catch(error => this.setState({
isLoading: false,
message: 'Something bad happened ’ + error
}));

I have a problem here can anybody help?

which? error message?

the error is “Uncaught SyntaxError: missing ) after argument list”

I have fixed the error, but still have error “Uncaught SyntaxError: Invalid or unexpected token”. I have check the token, the token is correct.

fetch(‘https://api.sales.com/v2/product/index’, {
method: ‘POST’,
withCredentials: true,
credentials: ‘include’,
headers: {
‘Authorization’: bearer,
‘Content-Type’: ‘application/json’,
‘token’:‘mytokencode21asda3qastq2a’,
‘productcode’:‘123434’
}
}).then(responseJson => {
var items = JSON.parse(responseJson._bodyInit);
})
.catch(error => this.setState({
isLoading: false,
message: 'Something bad happened ’ + error
}));

what is this error, JS or PHP?

Js. Its in console log.

That has nothing to do with the token you send. It is a syntax issue where something is not quoted or closed properly.

Use something like Postman or Fiddler to test the api.

ok thank you. I have to find another way, Yes better using Jquery

I think you misunderstood what I said. Try making the call in Postman or Fiddler first and verify that the API is functional before you blame your code for the issue.

After looking through your code again, you might want to check that this value is correct,
'productcode':'123434'

That isn’t something that would typically be in the header, but likely should be in the body of the request and may be the cause of the error.

I believe I already said to test their API in a tool. I also said, the product code was not in the correct spot.

Sponsor our Newsletter | Privacy Policy | Terms of Service