发布到外部网址使用ajax和php curl无法正常工作(Post to external url is not working using ajax and php curl)
我有第三方CRM的Web服务。 哪个使用html表单帖子工作正常
<form method='post' action='http://example.com/myservice/action'> <div id='JavascriptWarning' class='warningmessage'>Javascript must be enabled in order to complete this form</div> <span class='webformlabel'>Last Name</span> <input class='webforminput' name='LastName' type='text'><span class='mandatorymarker'>*</span><br> <span class='webformlabel'>First Name</span> <input class='webforminput' name='FirstName' type='text'><span class='mandatorymarker'>*</span><br> <input class='submitbutton' type='submit' id='SubmitButton' value='Submit' disabled='true' onclick='return Validate();'/> </form>
但我想在ajax或php curl post中使用它
当我使用ajax时,会出现错误消息
“请求的资源上没有'Access-Control-Allow-Origin'标头”这是我的代码
function StoreCRMData() { $.ajax({ type: "POST", url: "http://example.com/myservice/action", data:$('form').serialize(), success: function() { return false; } }); }
PHP卷曲也无法正常工作
<?php function post_to_url($url, $data) { $fields = ''; foreach($data as $key => $value) { $fields .= $key . '=' . $value . '&'; } rtrim($fields, '&'); $post = curl_init(); curl_setopt($post, CURLOPT_URL, $url); curl_setopt($post, CURLOPT_POST, count($data)); curl_setopt($post, CURLOPT_POSTFIELDS, $fields); curl_setopt($post, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($post); curl_close($post); } $data = array( "LastName" => "My First name", "FirstName" => "My Last name" ); post_to_url("http://example.com/myservice/action", $data); ?>
请帮我解决这个问题
I have a web service of a third party CRM. Which is working fine using a html form post
<form method='post' action='http://example.com/myservice/action'> <div id='JavascriptWarning' class='warningmessage'>Javascript must be enabled in order to complete this form</div> <span class='webformlabel'>Last Name</span> <input class='webforminput' name='LastName' type='text'><span class='mandatorymarker'>*</span><br> <span class='webformlabel'>First Name</span> <input class='webforminput' name='FirstName' type='text'><span class='mandatorymarker'>*</span><br> <input class='submitbutton' type='submit' id='SubmitButton' value='Submit' disabled='true' onclick='return Validate();'/> </form>
But I want to use it in ajax or php curl post
When I am using ajax there is error message
“No 'Access-Control-Allow-Origin' header is present on the requested resource” This is my code
function StoreCRMData() { $.ajax({ type: "POST", url: "http://example.com/myservice/action", data:$('form').serialize(), success: function() { return false; } }); }
Also PHP curl is not working
<?php function post_to_url($url, $data) { $fields = ''; foreach($data as $key => $value) { $fields .= $key . '=' . $value . '&'; } rtrim($fields, '&'); $post = curl_init(); curl_setopt($post, CURLOPT_URL, $url); curl_setopt($post, CURLOPT_POST, count($data)); curl_setopt($post, CURLOPT_POSTFIELDS, $fields); curl_setopt($post, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($post); curl_close($post); } $data = array( "LastName" => "My First name", "FirstName" => "My Last name" ); post_to_url("http://example.com/myservice/action", $data); ?>
Kindly help me how to solve this problem
原文:https://stackoverflow.com/questions/40915433
更新时间:2023-10-23 12:10