weixin_33701564 2017-09-13 09:20 采纳率: 0%
浏览 15

AJAX要求Linnworks

I am using Laravel to create an app for the Linnworks API. I have created my own AJAX requests in PHP that receive the data from javascript.

This is a working request.

Linnworks Example

inventoryItemId=28c6226d-08de-4eb5-9bf1-f6089972cf18&fieldName=0&fieldValue=sample string 1&locationId=e10f6f45-d4ad-4128-8b58-6ff84b23199a

Javascript Request to PHP

var itemArray = 
{
    "inventoryItemId": "39692932-f8b0-42d7-9419-0500d6d79769",
    "fieldName": "StockLevel",
    "fieldValue": 6,
    "locationId": "00000000-0000-0000-0000-000000000000"
};

var itemArrayStr = JSON.stringify(itemArray);

AjaxRequestNew({'dataArray': itemArrayStr, 'linnworksURL': 'Inventory/UpdateInventoryItemLevels'});

function AjaxRequestNew(ajaxData) {
$.ajax({
    headers: {'X-CSRF-TOKEN': mytoken},
    url: '/api-linnworks-request',
    method: 'post',
    data: ajaxData,
    async: false,
    complete: function (data) {
        resp = data.responseText;
    }
});
}

PHP AJAX Request

public function apiLinnworksRequest(Request $request){
$url ='https://eu1.linnworks.net//api/'.$request->linnworksURL;
$data = json_decode($request->dataArray);

$myvar = WebController::linnworksRequestNew($url, $data , $request);
}

public function linnworksRequestNew($url, $data , $request) {
$client = new Client();
// dd($data);

$res = $client->request(
    'POST', $url, [
    'headers' => [
        'Authorization' => $request->session()->get('LinnReturnToken')
    ],
    'form_params' => $data
]);
var_dump($res->getBody()->__toString());
}

This works and will update the stock levels of the item, however I need to update multiple items at the same time. I have found that Stock /SetStockLevel meets my requirements, however I can not format the request correctly and keep getting an error.

Linnworks Example

 stockLevels=[
{
    "SKU": "sample string 1",
    "LocationId": "2260b419-69dd-4586-b2b2-08db9591921c",
    "Level": 3
},
{
    "SKU": "sample string 1",
    "LocationId": "2260b419-69dd-4586-b2b2-08db9591921c",
    "Level": 3
}
]
  • 写回答

1条回答 默认 最新

  • weixin_33690367 2017-09-13 11:56
    关注

    Thanks to help from Linnworks and by using Postman, I have found that the request needs to be formatted in the following format.

    PHP

     'stocklevels' => 
         '[ { "SKU": "SUIT-4PC-[philip / paisley]-BLACK-02yrs", ]
         "LocationId": "00000000-0000-0000-0000-000000000000", 
         "Level": 3 }, { "SKU": "SUIT-4PC-[philip / paisley]-BLACK-03yrs", 
         "LocationId": "00000000-0000-0000-0000-000000000000", 
         "Level": 3 } ]'
    

    To replicate this I have sent the data from Javascript in the following format:

    JS

    var itemArray = 
        [
            {
                "SKU": "SUIT-4PC-[philip / paisley]-BLACK-02yrs",
                "LocationId": "00000000-0000-0000-0000-000000000000",
                "Level": 6
            },
            {
                "SKU": "SUIT-4PC-[philip / paisley]-BLACK-03yrs",
                "LocationId": "00000000-0000-0000-0000-000000000000",
                "Level": 6
            }
        ];
    
        var itemArrayStr = JSON.stringify(itemArray);
        var dataRequest = {stocklevels: itemArrayStr};
    
    评论

报告相同问题?

悬赏问题

  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真